⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 求一个数列的所有组合.txt

📁 c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了搜罗的,希望大家喜欢.
💻 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 + -