黑马程序员技术交流社区
标题:
谁能告诉我这是代码问题?还是题目问题?
[打印本页]
作者:
我不信命ZW
时间:
2015-7-10 21:50
标题:
谁能告诉我这是代码问题?还是题目问题?
这是我自己写的代码:仅供参考,可以优化的地方,麻烦留言一下。
package cn.itcast.io.p2.file.demo;
/**
* 1 2 3 4 5 6 7 8
* (a) - (b) = 1
* (c) - (d) = 2
* (e) + (f) = 7
* (g) + (h) = 9
* 每个数字用一次
* 1.条件,元素不能重复。
* 2.a至少是2,c至少是3。
*/
public class Test {
public static void main(String[] args){
int a,b,c,d,e,f,g,h;
int max=9,min=1;
int count=0;
/*由于a-b=1 所以a至少是+1*/
for(a=min+1;a<=max;a++){
for(b=min;b<=max;b++){
if(b==a){continue;}
/*由于c-d=2 所以c至少是+2*/
for(c=min+2;c<=max;c++){
if(c==a || c==b){continue;}
for(d=min;d<=max;d++){
if(d==a || d==b || d==c){continue;}
for(e=min;e<=max;e++){
if(e==a || e==b || e==c || e==d){continue;}
for(f=min;f<=max;f++){
if(f==a || f==b || f==c || f==d || f==e){continue;}
for(g=min;g<=max;g++){
if(g==a || g==b || g==c || g==d || g==e || g==f){continue;}
for(h=min;h<=max;h++){
if(h==a || h==b || h==c || h==d || h==e || h==f || h==g){continue;}
if( (a - b == 1) && (c - d == 2) && (e + f == 7) && (g + h == 9) ) {
count++;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
System.out.println("e="+e);
System.out.println("f="+f);
System.out.println("g="+g);
System.out.println("h="+h);
System.out.println("循环次数为:"+count);
return;
}
}
}
}
}
}
}
}
}
}
}
复制代码
QQ图片20150710201502.jpg
(72.3 KB, 下载次数: 2)
下载附件
2015-7-10 21:48 上传
题目
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2