欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一道小学奥数题的Java编程实现

程序员文章站 2022-04-01 08:52:41
...
2012华杯小中年级初赛试题的第一题解析
原题如下
一道小学奥数题的Java编程实现
            
    
    博客分类: 写给喜欢编程的小朋友 编程java 
程序源码


import java.util.HashSet;
import java.util.Set;

public class Sum {


	static private boolean isCollect(int a, int b, int c, int d, int e, int f) {
		int sum = a * 100 + b * 10 + c + d * 100 + e * 10 + f;

		if (sum > 1000)
			return false;

		int h = sum / 100;
		int j = (sum - h * 100) / 10;
		int k = sum % 10;
		Set s = new HashSet();
		s.add(a);
		s.add(b);
		s.add(c);
		s.add(d);
		s.add(e);
		s.add(f);
		s.add(h);
		s.add(j);
		s.add(k);

		boolean res = s.size() == 9 && !s.contains(0);
		if (res) {
			int all = h + j + k;
			System.out.println(sum + ":" + all);
		}
		return res;

	}

	static int dig[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	public static void main(String[] args) {
		int a, b, c, d, e, f, g, h, j, k;
		int total = 0;
		int count = 0;
		for (int i1 = 0; i1 < dig.length; i1++) {
			a = dig[i1];
			for (int i2 = 0; i2 < dig.length; i2++) {
				if (i1 == i2) {
					continue;
				}
				b = dig[i2];

				for (int i3 = 0; i3 < dig.length; i3++) {
					if (i3 == i2 || i3 == i1) {
						continue;
					}
					c = dig[i3];

					for (int i4 = 0; i4 < dig.length; i4++) {
						if (i4 == i1 || i4 == i2 || i4 == i3)
							continue;

						d = dig[i4];
						for (int i5 = 0; i5 < dig.length; i5++) {
							if (i5 == i1 || i5 == i2 || i5 == i3 || i5 == i4) {
								continue;
							}
							e = dig[i5];

							for (int i6 = 0; i6 < dig.length; i6++) {
								if (i6 == i1 || i6 == i2 || i6 == i3
										|| i6 == i4 || i6 == i5) {
									continue;
								}
								f = dig[i6];
								total++;
								if (isCollect(a, b, c, d, e, f)) {
									count++;
									System.out.println("answer is:" + a + b + c
											+ "," + d + e + f);
								}

							}
						}
					}

				}
			}
		}

		System.out.println("total check:" + total);
		System.out.println("answer count:" + count);

	}
}


答案是18

代码运行后输出如下
783:18
answer is:124,659
864:18
answer is:125,739
486:18
answer is:127,359
495:18
answer is:127,368
495:18
answer is:128,367
567:18
answer is:128,439
486:18
answer is:129,357
567:18
answer is:129,438
783:18
answer is:129,654
864:18
answer is:129,735
792:18
answer is:134,658
864:18
answer is:135,729
567:18
answer is:138,429
792:18
answer is:138,654
567:18
answer is:139,428
864:18
answer is:139,725
738:18
answer is:142,596
837:18
answer is:142,695
729:18
answer is:143,586
837:18
answer is:145,692
729:18
answer is:146,583
738:18
answer is:146,592
639:18
answer is:152,487
936:18
answer is:152,784
783:18
answer is:154,629
792:18
answer is:154,638
936:18
answer is:154,782
486:18
answer is:157,329
639:18
answer is:157,482
792:18
answer is:158,634
486:18
answer is:159,327
783:18
answer is:159,624
549:18
answer is:162,387
945:18
answer is:162,783
945:18
answer is:163,782
495:18
answer is:167,328
549:18
answer is:167,382
495:18
answer is:168,327
459:18
answer is:173,286
468:18
answer is:173,295
468:18
answer is:175,293
459:18
answer is:176,283
549:18
answer is:182,367
576:18
answer is:182,394
639:18
answer is:182,457
675:18
answer is:182,493
936:18
answer is:182,754
945:18
answer is:182,763
459:18
answer is:183,276
675:18
answer is:183,492
729:18
answer is:183,546
945:18
answer is:183,762
576:18
answer is:184,392
936:18
answer is:184,752
459:18
answer is:186,273
729:18
answer is:186,543
549:18
answer is:187,362
639:18
answer is:187,452
576:18
answer is:192,384
675:18
answer is:192,483
738:18
answer is:192,546
837:18
answer is:192,645
468:18
answer is:193,275
675:18
answer is:193,482
576:18
answer is:194,382
468:18
answer is:195,273
837:18
answer is:195,642
738:18
answer is:196,542
783:18
answer is:214,569
873:18
answer is:214,659
693:18
answer is:215,478
963:18
answer is:215,748
594:18
answer is:216,378
954:18
answer is:216,738
567:18
answer is:218,349
594:18
answer is:218,376
657:18
answer is:218,439
693:18
answer is:218,475
954:18
answer is:218,736
963:18
answer is:218,745
567:18
answer is:219,348
657:18
answer is:219,438
783:18
answer is:219,564
873:18
answer is:219,654
891:18
answer is:234,657
981:18
answer is:235,746
954:18
answer is:236,718
981:18
answer is:236,745
891:18
answer is:237,654
657:18
answer is:238,419
954:18
answer is:238,716
657:18
answer is:239,418
837:18
answer is:241,596
819:18
answer is:243,576
918:18
answer is:243,675
918:18
answer is:245,673
963:18
answer is:245,718
981:18
answer is:245,736
819:18
answer is:246,573
837:18
answer is:246,591
981:18
answer is:246,735
567:18
answer is:248,319
963:18
answer is:248,715
567:18
answer is:249,318
648:18
answer is:251,397
873:18
answer is:254,619
891:18
answer is:254,637
648:18
answer is:257,391
891:18
answer is:257,634
873:18
answer is:259,614
783:18
answer is:264,519
783:18
answer is:269,514
864:18
answer is:271,593
954:18
answer is:271,683
459:18
answer is:273,186
468:18
answer is:273,195
819:18
answer is:273,546
864:18
answer is:273,591
918:18
answer is:273,645
954:18
answer is:273,681
468:18
answer is:275,193
693:18
answer is:275,418
918:18
answer is:275,643
459:18
answer is:276,183
594:18
answer is:276,318
819:18
answer is:276,543
594:18
answer is:278,316
693:18
answer is:278,415
675:18
answer is:281,394
954:18
answer is:281,673
459:18
answer is:283,176
954:18
answer is:283,671
675:18
answer is:284,391
459:18
answer is:286,173
648:18
answer is:291,357
675:18
answer is:291,384
837:18
answer is:291,546
864:18
answer is:291,573
468:18
answer is:293,175
864:18
answer is:293,571
675:18
answer is:294,381
468:18
answer is:295,173
837:18
answer is:296,541
648:18
answer is:297,351
972:18
answer is:314,658
594:18
answer is:316,278
846:18
answer is:317,529
945:18
answer is:317,628
567:18
answer is:318,249
594:18
answer is:318,276
945:18
answer is:318,627
972:18
answer is:318,654
567:18
answer is:319,248
846:18
answer is:319,527
891:18
answer is:324,567
981:18
answer is:324,657
486:18
answer is:327,159
495:18
answer is:327,168
846:18
answer is:327,519
891:18
answer is:327,564
945:18
answer is:327,618
981:18
answer is:327,654
495:18
answer is:328,167
945:18
answer is:328,617
486:18
answer is:329,157
846:18
answer is:329,517
927:18
answer is:341,586
918:18
answer is:342,576
918:18
answer is:346,572
927:18
answer is:346,581
567:18
answer is:348,219
567:18
answer is:349,218
648:18
answer is:351,297
819:18
answer is:352,467
972:18
answer is:354,618
981:18
answer is:354,627
486:18
answer is:357,129
648:18
answer is:357,291
819:18
answer is:357,462
981:18
answer is:357,624
972:18
answer is:358,614
486:18
answer is:359,127
549:18
answer is:362,187
819:18
answer is:362,457
891:18
answer is:364,527
495:18
answer is:367,128
549:18
answer is:367,182
819:18
answer is:367,452
891:18
answer is:367,524
495:18
answer is:368,127
918:18
answer is:372,546
594:18
answer is:376,218
918:18
answer is:376,542
594:18
answer is:378,216
675:18
answer is:381,294
927:18
answer is:381,546
549:18
answer is:382,167
576:18
answer is:382,194
576:18
answer is:384,192
675:18
answer is:384,291
927:18
answer is:386,541
549:18
answer is:387,162
648:18
answer is:391,257
675:18
answer is:391,284
576:18
answer is:392,184
576:18
answer is:394,182
675:18
answer is:394,281
648:18
answer is:397,251
693:18
answer is:415,278
657:18
answer is:418,239
693:18
answer is:418,275
657:18
answer is:419,238
567:18
answer is:428,139
567:18
answer is:429,138
567:18
answer is:438,129
657:18
answer is:438,219
567:18
answer is:439,128
657:18
answer is:439,218
639:18
answer is:452,187
819:18
answer is:452,367
639:18
answer is:457,182
819:18
answer is:457,362
819:18
answer is:462,357
819:18
answer is:467,352
693:18
answer is:475,218
693:18
answer is:478,215
639:18
answer is:482,157
675:18
answer is:482,193
675:18
answer is:483,192
639:18
answer is:487,152
675:18
answer is:492,183
675:18
answer is:493,182
783:18
answer is:514,269
846:18
answer is:517,329
783:18
answer is:519,264
846:18
answer is:519,327
891:18
answer is:524,367
846:18
answer is:527,319
891:18
answer is:527,364
846:18
answer is:529,317
837:18
answer is:541,296
927:18
answer is:541,386
738:18
answer is:542,196
918:18
answer is:542,376
729:18
answer is:543,186
819:18
answer is:543,276
729:18
answer is:546,183
738:18
answer is:546,192
819:18
answer is:546,273
837:18
answer is:546,291
918:18
answer is:546,372
927:18
answer is:546,381
783:18
answer is:564,219
891:18
answer is:564,327
891:18
answer is:567,324
783:18
answer is:569,214
864:18
answer is:571,293
918:18
answer is:572,346
819:18
answer is:573,246
864:18
answer is:573,291
819:18
answer is:576,243
918:18
answer is:576,342
927:18
answer is:581,346
729:18
answer is:583,146
729:18
answer is:586,143
927:18
answer is:586,341
837:18
answer is:591,246
864:18
answer is:591,273
738:18
answer is:592,146
864:18
answer is:593,271
738:18
answer is:596,142
837:18
answer is:596,241
873:18
answer is:614,259
972:18
answer is:614,358
945:18
answer is:617,328
945:18
answer is:618,327
972:18
answer is:618,354
873:18
answer is:619,254
783:18
answer is:624,159
981:18
answer is:624,357
945:18
answer is:627,318
981:18
answer is:627,354
945:18
answer is:628,317
783:18
answer is:629,154
792:18
answer is:634,158
891:18
answer is:634,257
891:18
answer is:637,254
792:18
answer is:638,154
837:18
answer is:642,195
918:18
answer is:643,275
837:18
answer is:645,192
918:18
answer is:645,273
783:18
answer is:654,129
792:18
answer is:654,138
873:18
answer is:654,219
891:18
answer is:654,237
972:18
answer is:654,318
981:18
answer is:654,327
891:18
answer is:657,234
981:18
answer is:657,324
792:18
answer is:658,134
972:18
answer is:658,314
783:18
answer is:659,124
873:18
answer is:659,214
954:18
answer is:671,283
918:18
answer is:673,245
954:18
answer is:673,281
918:18
answer is:675,243
954:18
answer is:681,273
954:18
answer is:683,271
837:18
answer is:692,145
837:18
answer is:695,142
963:18
answer is:715,248
954:18
answer is:716,238
954:18
answer is:718,236
963:18
answer is:718,245
864:18
answer is:725,139
864:18
answer is:729,135
864:18
answer is:735,129
981:18
answer is:735,246
954:18
answer is:736,218
981:18
answer is:736,245
954:18
answer is:738,216
864:18
answer is:739,125
963:18
answer is:745,218
981:18
answer is:745,236
981:18
answer is:746,235
963:18
answer is:748,215
936:18
answer is:752,184
936:18
answer is:754,182
945:18
answer is:762,183
945:18
answer is:763,182
936:18
answer is:782,154
945:18
answer is:782,163
945:18
answer is:783,162
936:18
answer is:784,152
total check:60480
answer count:336

  • 一道小学奥数题的Java编程实现
            
    
    博客分类: 写给喜欢编程的小朋友 编程java 
  • 大小: 119.9 KB
相关标签: 编程 java