📄 求一个数列的所有组合.txt
字号:
求一个数列的所有组合[原创]
/*
to work out all the combination of an array
AUTHOR:BugEyes
http://BugEyes.blog.edu.cn
*/
#define N 5
#i nclude <conio.h>
void init(int arr[])
{ /* to initiate the array*/
int i;
for(i=0;i<N;i++)
arr[i]=i+1;
}
void next(int arr[])
{ /* to compute the next possible combination*/
int i;
for(i=N-1;i>=0&&arr[i]==1;i--)
arr[i]=0;
if(i>=0)
arr[i]=1;
}
int ZeroAtPos(int arr[])
{ /* return the position of 0 at the 'binary' array,if not,return -1*/
int i;
for(i=0;i<N;i++)
if(arr[i]==0)
return i;
return -1;
}
void output(int arr[],int binary[])
{ /* print the current combination according to 'binary' array*/
int i;
for(i=0;i<N;i++)
if(binary[i]==1)
printf("%4d",arr[i]);
printf("\n");
}
main()
{
int array[N];
int binary[N]={0};
clrscr();
init(array);
while(ZeroAtPos(binary)!=-1)
{
next(binary);
output(array,binary);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -