全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:7141
推到 Plurk!
推到 Facebook!

怎麼用C語言下去編寫排列組合

尚未結案
stardust0218
一般會員


發表:1
回覆:0
積分:0
註冊:2004-11-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-24 00:31:59 IP:163.13.xxx.xxx 未訂閱
我想用C寫出一個排列組合的程式 功能是 在26個英文字母中取出部份字母做排列 輸入一個數字(代表個數) 則程式會自動跑出該數字所有的字母排列組合 例如: 輸入1=>A 輸入2=>AB,BA 輸入3=>ABC,ACB,BAC,BCA,CAB,CBA ...等等 從0到26 由於我不是很懂C(幾乎不懂) 所以完全不知道該怎麼樣把程式架構起來 希望各位高手大大可以給我個方向 好讓我去想如何編寫 拜託<(_ _)> 發表人 - stardust0218 於 2004/11/24 00:40:59
allenchan
資深會員


發表:10
回覆:306
積分:283
註冊:2004-01-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-25 03:39:01 IP:61.62.xxx.xxx 未訂閱
這段程式碼是從 C 名題精選百則偷來的,加上一小點的修改,程式是利用旋轉法來排列所有組合: 比方說:A B C 的排列,結果如下: A B C C A B B C A B A C C B A A C B 旋轉一圈來排列所有的結果。
#include 
#include 
#include     #define   MAXSIZE   26
#define   ROTATE(p)           {  int  i, temp;                 temp = perm[p];               for (i = p-1; i >= 0; i--)    perm[i 1] = perm[i];          perm[0] = temp;            }    void main(void)
{
     int  perm[MAXSIZE];
     int  position;
     int  n;
     int  i;
     char line[100];         printf("\nPermutation by Rotation Method");
     printf("\n==============================");
     printf("\n\nNumber of Elements --> ");
     gets(line);
     n = atoi(line);
     n = (n < MAXSIZE ? n : MAXSIZE);         for (i = 0; i < n; i  )  /* initialize to 1,2,...,n  */
          perm[i] = i   1;         position = n - 1;
     while (position != 0) {  /* if still have positions..*/
          printf("\n");       /* display result           */
          for (i = 0; i < n; i  )
               printf("%c ", perm[i]   64);              position = n - 1;   /* starts from the last pos */
          ROTATE(position);   /* rotate them.             */
          while (perm[position] == position   1 && position != 0) {
               position--;    /* if last pos are equal and*/
               ROTATE(position); /* not zero, rotate again*/
          }
     }
     getch();
}    
發表人 - allenchan 於 2004/11/25 03:42:44
系統時間:2024-04-29 6:31:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!