这次终于遇到点挑战了,先上题目吧:
我的答案:
// trente.c
#include<stdio.h>
void test(int testnum,int limit);
void print();
int v,i,n,g,t,c,q,r,e;
int main()
{
t = 1;
r = 0;
v = 8;
i = 4;
test(9,9);
i = 9;
test(5,7);
v = 9;
i = 4;
test(2,3);
test(5,7);
return 0;
}
void test(int testnum,int limit)
{
int counti = 0;
int countj = 0;
int qa[] = {2,3,4};
int ea[] = {5,7,9};
int ga[] = {3,5,9};
int na[] = {9,8,6};
c = testnum;
while(c <= limit)
{
for(counti = 0;counti < 2;counti++)
{
q = qa[counti];
e = ea[counti];
for(countj = 0;countj < 2;countj ++)
{
g = ga[countj];
n = na[countj];
if(v * 10000 + i * 1000 + n * 100 + g * 10 + 1 + 2 * (c * 1000 + i * 100 + n * 10 + q ) == 100000 + e * 1000 + n * 100 + t * 10 + e)
{
//test to protect from repeat
print();
}
}
}
c++;
}
}
void print()
{
printf(" %d%d%d%d%d\n",v,i,n,g,t);
printf(" %d%d%d%d\n",c,i,n,q);
printf("+ %d%d%d%d\n",c,i,n,q);
printf("---------\n");
printf("%d%d%d%d%d%d\n",t,r,e,n,t,e);
printf("\n\n");
}
先说一句,问题我并没有完全解决,因为输出的答案中这九个数之间会有重复,而题目是不允许重复的。
那么想要正确答案的话就要在print调用之前先判断是不是有重复再进行输出。
但是我没有这么做,因为即使这样,我写的程序也不是最简便的,还有很多简化的方法。甚至可以不用计算机将所有数据分析出来。
标准答案我也不贴了,如果想要的人多的话我就敲一遍。
我想说的是,推理能力不是一时半会可以炼出来的,像柯南同学那样的真是太少了。
再说我觉得也没什么必要都分析出来,如果是考场上的话,我宁可让计算机多算一些时间,也不浪费自己的时间。
一家之言而已。