Algorithm 获取某个数字的所有组合
这可能是一个愚蠢的问题,答案很简单,但我一辈子都想不出来p 因此,如果我有一个变量Algorithm 获取某个数字的所有组合,algorithm,sorting,combinations,Algorithm,Sorting,Combinations,这可能是一个愚蠢的问题,答案很简单,但我一辈子都想不出来p 因此,如果我有一个变量n=2,例如,我想要一个列表,列出所有可以组合小于或等于n n=2的结果将是: 012 021 102 120 201 210 谢谢您的帮助:)这是一道数学题,但您正在尝试计算0-n的排列 要计算置换,请使用公式nPk,在您的例子中,k是所选的数字,n=k+1。你想取n的阶乘,然后除以n-k的阶乘n!/(n-k)!在您的示例中 3!/(3 - 2)! 3!/1! 3 * 2 * 1 / 1 = 6 下面的
n=2
,例如,我想要一个列表,列出所有可以组合小于或等于n
n=2
的结果将是:
012
021
102
120
201
210
谢谢您的帮助:)这是一道数学题,但您正在尝试计算0-n的排列 要计算置换,请使用公式nPk,在您的例子中,k是所选的数字,
n=k+1
。你想取n的阶乘,然后除以n-k的阶乘<代码>n!/(n-k)!代码>在您的示例中
3!/(3 - 2)!
3!/1!
3 * 2 * 1 / 1 = 6
下面的链接将详细介绍。
公共类编号组合{
public static void combination(int[] num, int x){//x is used to tell from which position in array permutations are needed to be done.
if(x!=num.length){
for(int i=x;i<num.length;i++){
int temp = num[i];
num[i] = num[x];
num[x] = temp;
combination(num,x+1);
temp = num[i];
num[i] = num[x];
num[x] = temp;
}
}
else{
for(int i=0;i<num.length;i++)
System.out.print(num[i]);
System.out.println();
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number:");
int n = Integer.parseInt(br.readLine());
int[] num = new int[n+1];
for(int i=0;i<=n;i++)
num[i] = i;
combination(num, 0);
}
publicstaticvoidcomposition(int[]num,intx){//x用于判断需要从数组中的哪个位置进行排列。
如果(x!=num.length){
对于(int i=x;iYou需要一个myarray。首先生成它很简单。对于int x=0,x我也喜欢玩我的老Korg Polysix。我编辑了答案,以便有足够的信息,可以通过示例导出算法,而链接仅支持它。