旋转排列法

来源:互联网 发布:电子商务模拟软件 编辑:程序博客网 时间:2024/05/16 15:44
Code:
  1.  #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define MAXSIZE 20  
  5. #define ROTATE(p) {int i,temp;       /  
  6.                    temp = perm[p];    /  
  7.                    for (i = p - 1; i >= 0; --i)      /  
  8.                             perm[i + 1] = perm[i];   /  
  9.                    perm[0] = temp;                /  
  10.                    }  
  11. int main()  
  12. {  
  13.     int perm[MAXSIZE];  
  14.     int position;  
  15.     int n;  
  16.     int i;  
  17.     char line[100];  
  18.       
  19.     printf("/nPermutation by rotation Method");  
  20.     printf("/n==============================");  
  21.     printf("/n/nNumber of Elements -->");  
  22.       
  23.     gets(line);  
  24.     n = atoi(line);  
  25.       
  26.     for (i = 0; i < n; ++i)  
  27.     {  
  28.         perm[i] = i + 1;  
  29.     }  
  30.       
  31.     position = n - 1;  
  32.       
  33.     while (position != 0)  
  34.     {  
  35.         printf("/n");  
  36.         for (i = 0; i < n; ++i)  
  37.         {  
  38.             printf("%d  ", perm[i]);  
  39.         }  
  40.           
  41.         position = n - 1;  
  42.         ROTATE(position);  
  43.           
  44.         while (perm[position] == position + 1 && position != 0)  
  45.         {  
  46.             --position;  
  47.             ROTATE(position);     
  48.         }  
  49.           
  50.     }  
  51. }  

 

原创粉丝点击