您的位置:首页 > 其它

12345678各取一个满足4个等式

2015-08-31 09:23 260 查看
/**
* ()+()=7 ()+()=9 ()-()=1 ()-()=2
*/
@Test
public void testStr() throws Exception{
String str = "";
String temp="";
for (int i=1;i<=8;i++) {
for (int j=1;j<=8;j++) {
temp=""+i;
temp+=j;
if(i+j==7) {
if(str.length()>0) {
str="";
}
str+=temp;
System.out.print("等于7的等式为:"+i + "+" + j + "=" + (i + j) + " ");
for (int k=1;k<=8;k++){
for (int l=1;l<=8;l++){
temp=""+k;
temp+=l;
if(k+l==9&&str.indexOf(""+k)==-1&&str.indexOf(""+l)==-1) {
if(str.length()>2) {
str=str.substring(0,2);
}
str+=temp;
System.out.print("等于9的等式为:"+k + "+" + l + "=" + (k + l) + " ");
for (int m=1;m<=8;m++){
for (int n=1;n<=8;n++){
temp=""+m;
temp+=n;
if(m-n==1&&str.indexOf(""+m)==-1&&str.indexOf(""+n)==-1) {
if(str.length()>4) {
str=str.substring(0,4);
}
System.out.print("等于1的等式为:"+m + "-" + n + "=" + (m - n) + " ");
str+=temp;
for (int o=1;o<=8;o++){
for (int p=1;p<=8;p++){
temp=""+o;
temp+=p;
if(o-p==2&&str.indexOf(""+o)==-1&&str.indexOf(""+p)==-1) {
if(str.length()>6) {
str=str.substring(0,6);
}
System.out.print("等于2的等式为:"+o + "-" + p + "=" + (o - p) + " ");
str+=temp;
}else {
continue;
}
}
}
}else {
continue;
}

}
}
System.out.println();
}else {
continue;
}
}
}
System.out.println();
}else{
str="";
continue;
}
}

}
System.out.println(str);
}

执行结果:

等于7的等式为:1+6=7 等于9的等式为:2+7=9 等于1的等式为:4-3=1

等于7的等式为:2+5=7 等于9的等式为:1+8=9 等于1的等式为:4-3=1 等于1的等式为:7-6=1

等于7的等式为:3+4=7 等于9的等式为:1+8=9 等于1的等式为:6-5=1

等于9的等式为:2+7=9 等于1的等式为:6-5=1

等于9的等式为:8+1=9 等于1的等式为:6-5=1

等于7的等式为:4+3=7 等于9的等式为:1+8=9 等于1的等式为:6-5=1

等于9的等式为:2+7=9 等于1的等式为:6-5=1

等于9的等式为:8+1=9 等于1的等式为:6-5=1

等于7的等式为:5+2=7 等于9的等式为:1+8=9 等于1的等式为:4-3=1 等于1的等式为:7-6=1

等于7的等式为:6+1=7 等于9的等式为:2+7=9 等于1的等式为:4-3=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: