四个数24 四种公式计算 24结果

public static ShuZi24 suanzhongjianleft24(int a,int b,int c,int d,int type){
    ShuZi24 sz=null;
    try {

        String fh[] = {"+", "-", "*", "÷"};
        for (int i = 0; i < 4; i++) {
            double  sum1 = jiSuan(a, b, i);
            for (int j = 0; j < 4; j++) {
               double sum2 = jiSuan(c,  sum1, j);
                for (int k = 0; k < 4; k++) {
                    double   sum = jiSuan( sum2, d, k);
                    //sum=Math.ceil(sum);
                    if (sum == 24) {
                        //System.out.print( sum1+"=="+sum2+"   ");
                        num++;


                       return  sendMess(a,b,c,d,fh,i,j,k,type);
                       // System.out.println("(" + c + "" + fh[j] + "(" + a + "" + fh[i] + "" + b + "))" + fh[k] + d+"="+sum);

                    }

                }

            }

        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}

public static ShuZi24 suanzhongjianright24(int a,int b,int c,int d,int type){

    try {

        String fh[] = {"+", "-", "*", "÷"};
        for (int i = 0; i < 4; i++) {
            double  sum1 = jiSuan(a, b, i);
            for (int j = 0; j < 4; j++) {
                double sum2 = jiSuan(  sum1,c, j);
                for (int k = 0; k < 4; k++) {
                    double   sum = jiSuan( d,sum2, k);
                    //sum=Math.ceil(sum);
                    if (sum == 24) {
                        //System.out.print( sum1+"=="+sum2+"   ");
                        num++;

                      return sendMess(a,b,c,d,fh,i,j,k,type);
                       //System.out.println(d+fh[k]+"(("+a+fh[i]+b+")"+fh[j]+c+")");

                    }

                }

            }

        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}


public static ShuZi24 suanLeft24(int a,int b,int c,int d,int type){
    String fh [] ={"+","-","*","÷"};
    double sum=0;
    for(int i=0;i<4;i++){
        double sum1=jiSuan(a,b,i);
        for(int j=0;j<4;j++){
           double sum2=jiSuan(sum1,c,j);
            for(int k=0;k<4;k++){
                sum=jiSuan(sum2,d,k);
                //sum=Math.ceil(sum);
                if(sum==24){
                    num++;
                   return sendMess(a,b,c,d,fh,i,j,k,type);
                   // System.out.println("(("+a+fh[i]+b+")"+fh[j]+c+"))"+fh[k]+d);
                }
            }
        }
    }
    return null;
}

public static ShuZi24 suanRight24(int a,int b,int c,int d,int type){
    String fh [] ={"+","-","*","÷"};
    double sum=0;
    for(int i=0;i<4;i++){
        double  sum1=jiSuan(a,b,i);
        for(int j=0;j<4;j++){
            double  sum2=jiSuan(c,sum1,j);
            for(int k=0;k<4;k++){
                sum=jiSuan(d,sum2,k);
               //sum=Math.ceil(sum);
              //  System.out.println(d+fh[k]+"("+c+fh[j]+"("+a+fh[i]+b+"))="+Math.ceil(sum));
                if(sum==24){
                    num++;
                 // System.out.println(d+fh[k]+"("+c+fh[j]+"("+a+fh[i]+b+"))="+sum);
                   return  sendMess(a,b,c,d,fh,i,j,k,type);
                }
            }
        }
    }
    return null;
}

public static double   jiSuan(double num1,double num2,int type){
    double sum=0;
    if(type==0){
        sum=num1+num2;

    }else if(type==1){
        sum=num1-num2;
    }else if(type==2){
        sum=num1*num2;
    }else{
       if(num2==0){
           return sum=0;
       }
        sum=num1/num2;
    }
    return sum;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值