Re: [其他] 求出一九位數
※ 引述《cheesesteak (牛排‧起司)》之銘言:
: 有一九位數abcdefghi 每個位數皆為1~9其中一數 且每個位數皆相異
: 若1|a, 2|ab (a為十位數 b為個位數), 3|abc..., 9|abcdefghi
: 即前k個數字組成的k位數皆可被k整除
: 求此九位數為?
跑程式(大學時學弟幫我寫的XD)告訴我 3 8 1 6 5 4 7 2 唯一解沒錯T^T
若要用討論就看自己喜好去分類了,能用的只有整除的性質去刪除
#include <stdio.h>
#define MAX 9
#define swap(x,y) t = x; x = y; y = t;
int p[MAX+1], i;
int test(int p[])
{
int t2, t3, t4, t5, t6, t7, t8;
t2 = p[2]%2;
t3 = (p[1]+p[2]+p[3])%3;
t4 = (p[3]*10+p[4])%4;
t5 = p[5]%5;
if((p[1]+p[2]+p[3]+p[4]+p[5]+p[6])%3 || p[6]%2)
t6 = 1;
else
t6 = 0;
t8 = (p[6]*100+p[7]*10+p[8])%8;
if(((p[1]+100*p[5]+10*p[6]+p[7])-(p[2]*100+p[3]*10+p[4]))%7 == 0)
t7 = 0;
else
t7 = 1;
if(t2||t3||t4||t5||t6||t7||t8)
return 0;
else
return 1;
}
void perm(int i)
{
int j, t;
if (i < MAX)
{
for (j = i; j <= MAX; ++j)
{
swap(p[i], p[j]);
perm(i+1);
swap(p[i], p[j]);
}
}
else
{
if(test(p))
{
for(j = 1; j <= MAX; ++j)
printf("%d", p[j]);
printf("\n");
getchar();
getchar();
}
}
}
main ()
{
for (i = 1; i <= MAX; ++i)
p[i] = i;
perm(1);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.175.10.75
※ 文章網址: http://www.ptt.cc/bbs/Math/M.1397015189.A.572.html
推
04/09 16:51, , 1F
04/09 16:51, 1F
討論串 (同標題文章)