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

📄 求n个元素的所有排列.txt

📁 c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了搜罗的,希望大家喜欢.
💻 TXT
字号:
求n个元素的所有排列[原创] 
/*
   to produce all the arrangement of a array
   AUTHOR:BugEyes
   http://BugEyes.blog.edu.cn
*/

#define N 3
#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 output(int arr[])
{  /* to output the array*/
  int i;
  for(i=0;i<N;i++)
    printf("%4d",arr[i]);
  printf("\n");
}

void sort(int arr[],int start)
{ /*to resort the array from the position 'start' to end*/
   int i,j;
   for(i=start;i<N;i++)
     for(j=start;j<N-1;j++)
       if(arr[j]>arr[j+1])
       {
          int t=arr[j];
          arr[j]=arr[j+1];
          arr[j+1]=t;
       }
}

void next(int arr[])
{  /* to produce the next arrangemeng*/
  int i,j;
  int temp;
  for(i=N-2;i>=0;i--)
    if(arr[i]<arr[i+1])
      break;
  for(j=N-1;j>i;j--)
    if(arr[j]>arr[i])
      break;
  if(i<0||j<0)
    exit(-1);
  temp=arr[i];
  arr[i]=arr[j];
  arr[j]=temp;
  sort(arr,i+1);
}

long jiecheng(int n)
{ /* to compute n!=n*(n-1)*(n-2)....*2*1 */
  long result=1;
  while(n>0)
    result*=n--;
  return result;
}

void main()
{
  int array[N];
  int count;
  clrscr();
  init(array);
  output(array);
  for(count=0;count<jiecheng(N)-1;count++)
  {
    next(array);
    output(array);
  }
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -