优美和式算法
在等式中,若数字0,1,2,3,4,5,6,7,8,9在a,b,c三个数中出现一次且只出现一次,这样的等式为优美和式,如:56+1987=2043,743+859=1602均为优美和式。设计程序求出所有的优美和式,并统计出优美和式的个数。
可能形式是a2b4c4和a3b3c4. 共有132个优美和式。
132个中包括了 a3 + b3 和 b3 + a3 即两数互换了位置的结果。
132个中没有包括 b4 + a2 的结果, 即两数互换位置的结果. 若加入,则增加 36种。
程序里在第一个打印语句下面加入下面两行即可.
(累加器加1,b和a 输出位置换一下)
n = n + 1;
printf("%d: %d+%d=%d\n",n,b,a,c);
C语言程序:
#include <stdio.h>
#include <stdlib.h>
main()
{
int a, b, c,i,j,n;
int ai[3],bi[4],ci[4];
// a2 b4 c4
n = 0;
for (a=10;a<=98;a++){
ai[0] = a / 10; ai[1] = a - ai[0] * 10;
if (ai[0] == ai[1]) goto Lab1;
for (b=1000;b<=9876;b++){
c = a + b;
if (c < 1000 || c >= 10000) goto Lab2;
bi[0] = b / 1000; bi[1] = (b - bi[0] * 1000) / 100;
bi[2] = (b - bi[0]*1000 - bi[1]*100) / 10;
bi[3] = b - bi[0] * 1000 - bi[1] * 100 -bi[2] * 10;
ci[0] = c / 1000; ci[1] = (c - ci[0] * 1000) / 100;
ci[2] = (c - ci[0]*1000 - ci[1]*100) / 10;
ci[3] = c - ci[0] * 1000 - ci[1] * 100 -ci[2] * 10;
for (i=0;i<2;i++){
for (j=0;j<=3;j++){if (ai[i] == bi[j]) goto Lab2;}
};
for (i=0;i<=2;i++){
for (j=i+1;j<=3;j++){
if (bi[i] == bi[j]) goto Lab2;
};
}; // end for i
for (i=0;i<=2;i++){
for (j=i+1;j<=3;j++){
if (ci[i] == ci[j]) goto Lab2;
};
}; // end for i
for (i=0;i<=3;i++){
for (j=0;j<=3;j++){if (ci[i] == bi[j]) goto Lab2;}
for (j=0;j<=1;j++){if (ci[i] == ai[j]) goto Lab2;}
};
n = n + 1;
printf("%d: %d+%d=%d\n",n,a,b,c);
Lab2:;
};
Lab1:;
};
// a3 b3 c4
for (a=100;a<=987;a++){
ai[0] = a / 100; ai[1] = (a - ai[0] * 100) / 10;
ai[2] = a - ai[0]*100 - ai[1]*10;
if (ai[0] == ai[1] || ai[0] == ai[2] || ai[1] == ai[2]) goto Lab11;
for (b=100;b<=987;b++){
c = a + b;
if (c < 1000 || c >= 10000) goto Lab22;
bi[0] = b / 100; bi[1] = (b - bi[0] * 100) / 10;
bi[2] = b - bi[0]*100 - bi[1]*10;
if (bi[0] == bi[1] || bi[0] == bi[2] || bi[1] == bi[2]) goto Lab22;
ci[0] = c / 1000; ci[1] = (c - ci[0] * 1000) / 100;
ci[2] = (c - ci[0]*1000 - ci[1]*100) / 10;
ci[3] = c - ci[0] * 1000 - ci[1] * 100 -ci[2] * 10;
for (i=0;i<=2;i++){
for (j=i+1;j<=3;j++){
if (ci[i] == ci[j]) goto Lab22;
};
}; // end for i
for (i=0;i<=2;i++){
for (j=0;j<=2;j++){if (ai[i] == bi[j]) goto Lab22;}
};
for (i=0;i<=3;i++){
for (j=0;j<=2;j++){if (ci[i] == bi[j]) goto Lab22;}
for (j=0;j<=2;j++){if (ci[i] == ai[j]) goto Lab22;}
};
n = n + 1;
printf("%d: %d+%d=%d\n",n,a,b,c);
Lab22:;
};
Lab11:;
};
}
答案:
1: 26+4987=5013
2: 27+4986=5013
3: 34+5978=6012
4: 34+5987=6021
5: 37+5984=6021
6: 38+5974=6012
7: 43+5978=6021
8: 47+2968=3015
9: 48+2967=3015
10: 48+5973=6021
11: 56+1978=2034
12: 56+1987=2043
13: 57+1986=2043
14: 58+1976=2034
15: 64+2987=3051
16: 65+1978=2043
17: 67+2948=3015
18: 67+2984=3051
19: 68+1975=2043
20: 68+2947=3015
21: 73+5948=6021
22: 74+5938=6012
23: 75+1968=2043
24: 76+1958=2034
25: 78+1956=2034
26: 78+1965=2043
27: 78+5934=6012
28: 78+5943=6021
29: 84+2967=3051
30: 84+5937=6021
31: 86+1957=2043
32: 86+4927=5013
33: 87+1956=2043
34: 87+2964=3051
35: 87+4926=5013
36: 87+5934=6021
37: 246+789=1035
38: 249+786=1035
39: 264+789=1053
40: 269+784=1053
41: 284+769=1053
42: 286+749=1035
43: 289+746=1035
44: 289+764=1053
45: 324+765=1089
46: 325+764=1089
47: 342+756=1098
48: 346+752=1098
49: 347+859=1206
50: 349+857=1206
51: 352+746=1098
52: 356+742=1098
53: 357+849=1206
54: 359+847=1206
55: 364+725=1089
56: 365+724=1089
57: 423+675=1098
58: 425+673=1098
59: 426+879=1305
60: 429+876=1305
61: 432+657=1089
62: 437+589=1026
63: 437+652=1089
64: 439+587=1026
65: 452+637=1089
66: 457+632=1089
67: 473+589=1062
68: 473+625=1098
69: 475+623=1098
70: 476+829=1305
71: 479+583=1062
72: 479+826=1305
73: 483+579=1062
74: 487+539=1026
75: 489+537=1026
76: 489+573=1062
77: 537+489=1026
78: 539+487=1026
79: 573+489=1062
80: 579+483=1062
81: 583+479=1062
82: 587+439=1026
83: 589+437=1026
84: 589+473=1062
85: 623+475=1098
86: 624+879=1503
87: 625+473=1098
88: 629+874=1503
89: 632+457=1089
90: 637+452=1089
91: 652+437=1089
92: 657+432=1089
93: 673+425=1098
94: 674+829=1503
95: 675+423=1098
96: 679+824=1503
97: 724+365=1089
98: 725+364=1089
99: 742+356=1098
100: 743+859=1602
101: 746+289=1035
102: 746+352=1098
103: 749+286=1035
104: 749+853=1602
105: 752+346=1098
106: 753+849=1602
107: 756+342=1098
108: 759+843=1602
109: 764+289=1053
110: 764+325=1089
111: 765+324=1089
112: 769+284=1053
113: 784+269=1053
114: 786+249=1035
115: 789+246=1035
116: 789+264=1053
117: 824+679=1503
118: 826+479=1305
119: 829+476=1305
120: 829+674=1503
121: 843+759=1602
122: 847+359=1206
123: 849+357=1206
124: 849+753=1602
125: 853+749=1602
126: 857+349=1206
127: 859+347=1206
128: 859+743=1602
129: 874+629=1503
130: 876+429=1305
131: 879+426=1305
132: 879+624=1503