12345678各取一个满足4个等式

/**
* ()+()=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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值