早教吧 育儿知识 作业答案 考试题库 百科 知识分享

这个程序要求是输出m个数中任取n个数的所有组合,我要抓狂了==#include;#includeinta[100];voidcomb(intm,intn){inti=0;inth=0;for(i=m;i>=n;i--){a[n]=i;if(n>1){comb(i-1,n-1);}else{for(h=n;h>0;h--){printf("%d",&a[h]);}printf

题目详情
这个程序要求是输出m个数中任取n个数的所有组合,我要抓狂了= =
#include ;
#include
int a[100];
void comb(int m,int n){
int i=0;
int h=0;
for( i=m;i>=n;i--){
a[n]=i;
if(n>1){
comb(i-1,n-1);
}
else{
for(h=n;h>0;h--){
printf("%d",&a[h]);
}
printf("\n");
}
}
system("PAUSE");
}
void main(){
int m,n;
printf("please input the total number and the number you want to choose:");
scanf("%d%d",&m,&n);
comb(m,n);
}
▼优质解答
答案和解析
个人看法:
程序中数组作用域是全局的,递归中会改变数组;else中输出时是不是改为printf("%d",a[h]),之前输出的好像是地址,我对printf函数不熟悉,可以直接cout.
建议换算法.用与数组等位数的二进制表示是否输出,1输出,0不输出.所有的二进制数列完,就输出完毕了.
下面是一个集合的所有子集输出,看看吧:
#include
#include
using namespace std;
int main()
{
int size;
cout size;
int * array = new int[ size ];
char * str = new char[ size + 1 ];
for( int i = 0; i < size; i ++ )
array[ i ] = i;
cout