回复 10楼 laoyang103
我从先改一下把数组a[5]={1,2,3,4,5};
我输入2 应该打印
1 2
1 3
1 4
...
4 5
我输入3 打印
1 2 3
1 2 4
1 2 5
....
3 4 5
我输入4 打印
1 2 3 4
1 2 3 5
1 3 4 5
...
2 3 4 5
依次类推(如果数组有10个数呢)
我的意思就是这样
#include <stdio.h> #include <string.h> int data[100] = {1,2,3,4,5,6,7,8,9,10}; bool foot[100] = {0}; int cool = 0; void com(int n,int k,int mem[],int depth,int begin,int pos) { int i,j; if(k == depth) { for(i = 0;i<k;i++) printf("%d ",mem[i]); cool++; printf("\n"); return ; } for(i = pos;i<n;i++) { if(!foot[i]) { foot[i] = true; mem[begin] = data[i]; com(n,k,mem,depth+1,begin+1,i+1); foot[i] = false; } } } int main() { int mem[100] = {0}; int m,n; while(~scanf("%d%d",&m,&n)) { memset(mem,0,sizeof(mem)); memset(foot,0,sizeof(foot)); com(m,n,mem,0,0,0); printf("\n"); } return 0; }