求一个数列的所有组合.txt
来自「c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了」· 文本 代码 · 共 59 行
TXT
59 行
求一个数列的所有组合[原创]
/*
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 + =
减小字号Ctrl + -
显示快捷键?