将0,1,2,3,4,5,6,7,8,9共10个数字填入下面的乘法公式中, 代替*号, 每个数字只能用2次,总共20个*刚刚好。
*** (被乘数)
*** (乘数)
------
***
***
***
--------
***** (积)
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
int a, b, c, d, ok;
int i, j, k, out=1;
int cnt[10];
clock_t s, t;
s = clock();
for(i=100; i<=299 && out; i++)
{
for(j=112; j<=998 && out; j++)
{
if(j%100/10 == 0 || j % 10 == 0 || i % 10 == 0)
continue;
memset(cnt, 0, sizeof(cnt));
cnt[i/100]++;
cnt[i%100/10]++;
cnt[i%10]++;
cnt[j/100]++;
cnt[j%100/10]++;
cnt[j%10]++;
a = j % 10 * i;
if(a > 999)
continue;
cnt[a/100]++;
cnt[a%100/10]++;
cnt[a%10]++;
b = j % 100 / 10 * i;
if(b > 999)
continue;
cnt[b/100]++;
cnt[b%100/10]++;
cnt[b%10]++;
c = j / 100 * i;
if(c > 999)
continue;
cnt[c/100]++;
cnt[c%100/10]++;
cnt[c%10]++;
d = a + b * 10 + c * 100;
if(d < 100000)
{
cnt[d/10000]++;
cnt[d%10000/1000]++;
cnt[d%1000/100]++;
cnt[d%100/10]++;
cnt[d%10]++;
ok = 1;
for(k=0; k<=9; k++)
{
if(cnt[k] > 2)
{
ok = 0;
break;
}
}
if(ok == 1)
{
printf("%d %d\n", i, j);
out = 0;
}
}
}
}
t = clock();
printf("time: %dMS\n", t-s);
return 0;
}
|
阅读(558) | 评论(1) | 转发(0) |