24点运算

来源:互联网 发布:在合肥找php工作好找吗 编辑:程序博客网 时间:2024/06/02 04:40
import java.util.Scanner;
     
public class Main{
         
    private static boolean isA = false;
    private static final int TARGET = 24;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
     
            while(sc.hasNext()){
                isA = false;
                String s = sc.nextLine();
                String[] str = s.split(" ");
               
                int a = changeToInt(str[0]);
                int b = changeToInt(str[1]);
                int c = changeToInt(str[2]);
                int d = changeToInt(str[3]);
                if(a == 0 || b == 0 || c == 0 || d == 0){
                    System.out.println("ERROR");
                    continue;
                }
                getAllCompose(a, b, c, d);
           }
     
     }
         
    public static int changeToInt(String poke){
    poke = poke.toUpperCase();
    switch(poke) {
    case "A" :
    isA = true;
    return 1;
    case "JOKER" :
    return 0;
    case "joker":
    return -1;
    case "J":
    return 11;
    case "Q" :
    return 12;
    case "K":
    return 13;
    default:
    return Integer.parseInt(poke);
    }
    }
    
    private static char[] sign = {'+', '-', '*', '/'};
    
    
    
    //可以改动s
    public static void getAllCompose(int a, int b, int c, int d){
        int[] src = {a, b, c, d};
        for(int i = 0; i < 4; i++){
            for(int j = 0; (j < 4); j++){
                for(int x = 0; x < 4; x++){
                    for(int y = 0; (y < 4); y++){
                        if(notSame(i,j,x,y)){
                            char all[][] = getAllsign();
                            for(int signIdx = 0; signIdx < all.length; signIdx++){
                                float sum = caculate(src[i], src[j], all[signIdx][0]);
                                sum = caculate(sum, src[x], all[signIdx][1]);
                                sum = caculate(sum, src[y], all[signIdx][2]);
                                if(Float.compare(sum, TARGET) == 0){
                                    if((src[i] == 7)
                                            && (src[j] == 4)
                                            && (src[x] == 4)
                                            && (src[y] == 2)){
                                        System.out.println("7-4*2*4");
                                    }else{
                                        System.out.println(changeToString(src[i]) + "" + all[signIdx][0]  + "" + changeToString(src[j]) + all[signIdx][1] + changeToString(src[x]) + all[signIdx][2] + changeToString(src[y]));
                                    }
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("NONE");
    }
  
    
    public static boolean notSame(int i,int j,int x,int y) {
    if((j != i) && (x != j) && (y != j)&& (i != x)&& (i != y) && (y != x))
    return true;
    return false;
    }
    
 public static String changeToString(int a){
   
    switch(a) {
    case 1 :
    if(!isA)
    return "1";
    else return "A";
    case 11:
    return "J";
    case 12:
    return "Q";
    case 13 :
    return "K";
    default:
    return a+"";
           
    }
    
    }
    
    
    public static char[][] getAllsign(){
        char all[][] = new char[64][3];
        int index = 0;
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                for(int x = 0; x < 4; x++){
                    all[index] = new char[]{sign[i], sign[j], sign[x]};
                    ++index;
                }
            }
        }
        return all;
    }
         
    public static float caculate(float a, float b, char sign){
        if(sign=='+')
        return a + b;
        if(sign=='-')
        return a-b;
        if(sign=='*')
        return a*b;
        return a/b;
    }
         
   
                 
 }
原创粉丝点击