题解 | #24点运算#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import java.io.*;
import java.util.*;
public class Main {
private static String[] op = new String[]{"+","-","*","/"};
public static void main(String[] args)throws IOException{
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
String input;
while((input = re.readLine())!=null && !"".equals(input)){
String[] ss = input.split(" ");
int a = getInputNum(ss[0]);
int b = getInputNum(ss[1]);
int c = getInputNum(ss[2]);
int d = getInputNum(ss[3]);
if(a== -1 || b==-1 ||c == -1||d == -1){
System.out.println("ERROR");
continue;
}
compute(a,b,c,d);
}
}
public static int getInputNum(String str){
switch(str.toUpperCase()){
case "A":
return 1;
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "JOKER":
return -1;
default:
return Integer.parseInt(str);
}
}
public static void compute(int a,int b,int c,int d){
int[]arr = {a,b,c,d};
String[][] arr1 = symbol();
for(int i =0;i<4;i++){
for(int j =0;j<4;j++){
for(int k =0;k<4;k++){
for(int p =0;p<4;p++){
if((i!=j)&&(i!=k)&&(i!=p)&&(j!=k)&&(j!=p)&&(k!=p)){
for(String[]str :arr1){
int sum =sumNum(arr[i],arr[j],str[0]);
sum =sumNum(sum,arr[k],str[1]);
sum =sumNum(sum,arr[p],str[2]);
if(sum == 24){
String str1=getInputString(arr[i])+str[0]+getInputString(arr[j])+str[1]+getInputString(arr[k])+str[2]+getInputString(arr[p])+"";
System.out.println(str1);
return;
}
}
}
}
}
}
}
System.out.println("NONE");
}
public static String getInputString(int i){
switch(i){
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return String.valueOf(i);
}
}
public static String[][]symbol(){
String[][] symbol = new String[64][3];
int p = 0;
for(int i =0;i<4;i++){
for(int j =0;j<4;j++){
for(int k =0;k<4;k++){
symbol[p++] = new String[]{op[i],op[j],op[k]};
}
}
}
return symbol;
}
public static int sumNum(int a,int b,String symb){
switch(symb){
case "+":
return a+b;
case "-":
return a-b;
case "*":
return a*b;
case "/":
return a/b;
default:
return 0;
}
}
}
import java.util.*;
public class Main {
private static String[] op = new String[]{"+","-","*","/"};
public static void main(String[] args)throws IOException{
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
String input;
while((input = re.readLine())!=null && !"".equals(input)){
String[] ss = input.split(" ");
int a = getInputNum(ss[0]);
int b = getInputNum(ss[1]);
int c = getInputNum(ss[2]);
int d = getInputNum(ss[3]);
if(a== -1 || b==-1 ||c == -1||d == -1){
System.out.println("ERROR");
continue;
}
compute(a,b,c,d);
}
}
public static int getInputNum(String str){
switch(str.toUpperCase()){
case "A":
return 1;
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "JOKER":
return -1;
default:
return Integer.parseInt(str);
}
}
public static void compute(int a,int b,int c,int d){
int[]arr = {a,b,c,d};
String[][] arr1 = symbol();
for(int i =0;i<4;i++){
for(int j =0;j<4;j++){
for(int k =0;k<4;k++){
for(int p =0;p<4;p++){
if((i!=j)&&(i!=k)&&(i!=p)&&(j!=k)&&(j!=p)&&(k!=p)){
for(String[]str :arr1){
int sum =sumNum(arr[i],arr[j],str[0]);
sum =sumNum(sum,arr[k],str[1]);
sum =sumNum(sum,arr[p],str[2]);
if(sum == 24){
String str1=getInputString(arr[i])+str[0]+getInputString(arr[j])+str[1]+getInputString(arr[k])+str[2]+getInputString(arr[p])+"";
System.out.println(str1);
return;
}
}
}
}
}
}
}
System.out.println("NONE");
}
public static String getInputString(int i){
switch(i){
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return String.valueOf(i);
}
}
public static String[][]symbol(){
String[][] symbol = new String[64][3];
int p = 0;
for(int i =0;i<4;i++){
for(int j =0;j<4;j++){
for(int k =0;k<4;k++){
symbol[p++] = new String[]{op[i],op[j],op[k]};
}
}
}
return symbol;
}
public static int sumNum(int a,int b,String symb){
switch(symb){
case "+":
return a+b;
case "-":
return a-b;
case "*":
return a*b;
case "/":
return a/b;
default:
return 0;
}
}
}