Re: [問題] 排列組合1~9的所有可能
看板C_and_CPP (C/C++)作者Powernow (PowerNow!)時間16年前 (2010/04/15 00:39)推噓0(0推 0噓 0→)留言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
討論串 (同標題文章)
完整討論串 (本文為第 5 之 5 篇):
3
11
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章