Re: [問題] 排列組合1~9的所有可能

看板C_and_CPP (C/C++)作者 (PowerNow!)時間16年前 (2010/04/15 00:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/5 (看更多)
直覺的寫法,沒有講求效率... #include <stdio.h> #define ELEMENT_NUM 9 int isrepeated(int *array) { int i, j; for (i = 0; i < ELEMENT_NUM; i++) for (j = i + 1; j < ELEMENT_NUM; j++) if (array[i] == array[j]) return 0; return 1; } void permutate(int element) { int i, j; static int result[ELEMENT_NUM]; if (element == ELEMENT_NUM && isrepeated(result)) { for (i = 0; i < ELEMENT_NUM; i++) printf("%d", result[i]); printf("\n"); } if (element < ELEMENT_NUM + 1) for (j = 1; j < ELEMENT_NUM + 1; j++) { result[element] = j; permutate(element + 1); } } void main() { permutate(0); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.128.220.49
文章代碼(AID): #1BnU-abt (C_and_CPP)
文章代碼(AID): #1BnU-abt (C_and_CPP)