java程序问题,从n个数中选取r个数的组合
publicclassCombination{staticString[][]tree=newString[10][10];staticintbranch=0;publi...
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<tree.length;i++){
if(tree[i].length!=0){
for(int m=0;m<tree[i].length;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].length;i++){
tree[treeNumber][i]=String.valueOf(n)+tree[treeNumber][i];
}
int l=0;
for(int i=0;i<tree[fromTree].length;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<tree[fromTree].length;i++){
while(tree[fromTree][i]==null){
tree[fromTree][i]=tree[treeNumber][m];
m++;
}
}
}else{
System.out.println("输入数据有误");
}
}
}
大神帮我看看我的程序哪里有问题? 展开
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<tree.length;i++){
if(tree[i].length!=0){
for(int m=0;m<tree[i].length;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].length;i++){
tree[treeNumber][i]=String.valueOf(n)+tree[treeNumber][i];
}
int l=0;
for(int i=0;i<tree[fromTree].length;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<tree[fromTree].length;i++){
while(tree[fromTree][i]==null){
tree[fromTree][i]=tree[treeNumber][m];
m++;
}
}
}else{
System.out.println("输入数据有误");
}
}
}
大神帮我看看我的程序哪里有问题? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询