两位数乘三位数等于四位数并且这九个数都不重复

问题描述和思路

两位数乘三位数等于四位数并且这九个数都不重复

有一点技巧 因为我没有专门训练过ACM 觉得这个还是挺巧妙的 留个纪念

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
int isOK(int a)
{
int i = 0;
int x = 0;
int add = 0;
int xor = 0;
/* 12 345 6789 */
for (i = 0; i < 9; i++)
{
x = a % 10;
a /= 10;
x = 1 << x; /* change to bin */
xor ^= x;
add += x;
}
/* e.g., for bin, 10(2)+11(2) = 5 > 10(2)^11(2) = 1 while 01(2)+10(2) == 01(2)^10(2) */
if (add == xor)
{
return 1;
}
return 0;
}
int main(void)
{
unsigned int x = 0;
unsigned int y = 0;
unsigned int result = 0;
for (x = 10; x < 100; x++)
{
for (y = 100; y < 1000; y++)
{
result = x * y;
if (result < 10000 && result >= 1000)
{
if (isOK(x*10000000+y*10000+result))
{
printf("%d×%d=%d\t%d\n", x, y, result, x*10000000+y*10000+result);
}
}
}
}
return 0;
}

结果如下

1 12×483=5796 124835796
2 12×695=8340 126958340
3 13×406=5278 134065278
4 13×546=7098 135467098
5 13×604=7852 136047852
6 13×754=9802 137549802
7 15×326=4890 153264890
8 15×486=7290 154867290
9 15×492=7380 154927380
10 15×632=9480 156329480
11 15×648=9720 156489720
12 18×297=5346 182975346
13 18×392=7056 183927056
14 18×409=7362 184097362
15 19×204=3876 192043876
16 19×253=4807 192534807
17 19×402=7638 194027638
18 19×453=8607 194538607
19 23×196=4508 231964508
20 26×345=8970 263458970
21 27×198=5346 271985346
22 28×157=4396 281574396
23 32×169=5408 321695408
24 34×178=6052 341786052
25 34×185=6290 341856290
26 38×159=6042 381596042
27 39×186=7254 391867254
28 42×138=5796 421385796
29 48×159=7632 481597632
30 48×165=7920 481657920
31 54×168=9072 541689072
32 59×108=6372 591086372
33 59×136=8024 591368024
34 63×154=9702 631549702
35 69×108=7452 691087452