TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现:

- public class Test73{
- public static void main(String args[]){
- findTheNumber73();
- }
- /**
- * 73.和数能表示1-23的5个正整数
- */
- public static void findTheNumber73() {
- for (int a = 1; a < 23; a++)
- for (int b = a + 1; b < 23; b++)
- for (int c = b + 1; c < 23; c++)
- for (int d = c + 1; d < 23; d++)
- for (int e = d + 1; e < 23; e++) {
-
- if (a + b + c + d + e != 23) // 互不相同
- continue;
-
- boolean oneIsOK = false;
- boolean allIsOK = true;
- int max = 1;
- for (int s = 1; s <= 23; s++) {
- oneIsOK = false;
-
- for (int a1 = 0; a1 <= max; a1++)
- for (int b1 = 0; b1 <= max; b1++)
- for (int c1 = 0; c1 <= max; c1++)
- for (int d1 = 0; d1 <= max; d1++)
- for (int e1 = 0; e1 <= max; e1++) {
- // 只要其中有一组成立即可
- if (a1 * a + b1 * b + c1
- * c + d1 * d + e1
- * e == s) {// 能组成
- oneIsOK = true;
- // System.out.println(a1
- // + "X" + a + "+"
- // + b1 + "X" + b
- // + "+" + c1
- // + "X" + c + "+"
- // + d1 + "X" + d
- // + "+" + e1
- // + "X" + e + "="
- // + s);
- }
- }
-
- // 如果有一组不成立则本组否定
- if (oneIsOK == false) {
- allIsOK = false;
- }
-
- }// for(s)
- if (allIsOK == true) {
- System.out.print(" a=" + a);
- System.out.print(" b=" + b);
- System.out.print(" c=" + c);
- System.out.print(" d=" + d);
- System.out.print(" e=" + e);
- System.out.println();
- }
- }
- }
- }
- C:at>java Test73
- a=1 b=2 c=3 d=5 e=12
- a=1 b=2 c=3 d=6 e=11
- a=1 b=2 c=3 d=7 e=10
- a=1 b=2 c=4 d=5 e=11
- a=1 b=2 c=4 d=6 e=10
- a=1 b=2 c=4 d=7 e=9
- C:at>
复制代码
源码下载:http://file.javaxxz.com/2014/11/21/000512421.zip |
|