该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public class Combination {
static String[][] tree=new String[10][10];
static int branch=0;
public static void main(String[] args){
System.out.println("找出从自然数 1, 2, …, n 中任取r个数的所有组合");
System.out.println("输入n,r");
Scanner c=new Scanner(System.in);
int n=c.nextInt();
Scanner c1=new Scanner(System.in);
int r=c1.nextInt();
find(n,r,0,0);
System.out.println(tree.length);
for(int i=0;i
if(tree[i].length!=0){
for(int m=0;m
if(tree[i][m].length()==r){
System.out.println(tree[i][m]);
}
}
}
}
}
public static void find(int n,int r,int treeNumber,int fromTree){
if(r==1){
for(int i=1;i<=n;i++){
tree[fromTree][i-1]=String.valueOf(i);
}
}else if(n==r){
String temp="";
for(int i=r;i>0;i--){
temp=String.valueOf(i)+temp;
}
tree[fromTree][0]=temp;
}else if(n>r){
branch++;
find(n-1,r-1,branch,treeNumber);
for(int i=0;i
tree[treeNumber][i]=String.valueOf(n)+tree[treeNumber][i];
}
int l=0;
for(int i=0;i
while(tree[fromTree][i]==null){
tree[fromTree][i]=tree[treeNumber][l];
l++;
}
}
branch++;
find(n-1,r,branch,treeNumber);
int m=0;
for(int i=0;i
while(tree[fromTree][i]==null){
tree[fromTree][i]=tree[treeNumber][m];
m++;
}
}
}else{
System.out.println("输入数据有误");
}
}
}
大神帮我看看有什么问题~~~~