ccf/csp 201903-2 二十四点
本题我写了两种思路,一种是采用栈,另一种是调用java的arraylist。我将两种代码和思路都写在了下面~
首先是题目如下:
首先是第一种方法,采用栈的结构。建立两个栈,一个栈叫stack存放运算符,另一个栈叫nums存放数字。运算优先级是乘法除法>加法减法,所以我的思路是,边输入边将数据分类存放进栈,如果输入到运算符x和/,就将数字栈里的最顶层数字pop出来与该运算符的下一位数字进行运算,并将所得运算结果存入数字栈,依次迭代。
代码如下:
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String str = sc.next();
if (is_24(str))
System.out.println("Yes");
else
System.out.println("No");
}
}
public static boolean is_24(String str) {
Stack<Character> stack = new Stack<Character>(); //stack用于保存符号
Stack<Integer> nums = new Stack<Integer>(); //nums用于保存数字
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '+') //如果是加减运算把+和-号压进stack栈
stack.push(str.charAt(i) );
else if (str.charAt(i) == '-')
stack.push(str.charAt(i) );
else if (str.charAt(i) == 'x') { //如果是乘除运算,取输入的后一位和nums栈的pop做运算,再将所得结果压入nums栈
int flag = Integer.parseInt(str.charAt(i + 1)+"") ; //char+string=string
//str.charAt(i + 1)是char,""是string
int temp = nums.pop() * flag ;
nums.push(temp);
i++;
} else if (str.charAt(i) == '/') {
int flag = Integer.parseInt(str.charAt(i + 1)+"") ;
int temp = nums.pop() / flag;
nums.push(temp);
i++;
} else if (str.charAt(i) <= '9' && str.charAt(i) >= '0') //如果是数字压入nums栈
nums.push(Integer.parseInt(str.charAt(i )+""));
}
while (!stack.isEmpty()) {
if (stack.peek().equals('-'))
count += nums.pop() * (-1);
else if (stack.peek().equals('+'))
count += nums.pop();
stack.pop();
}
count += nums.pop();
if (count == 24)
return true;
else
return false;
}
}
第二种是模仿平常的加减乘除运算过程(先做乘除运算得到中间结果,再做加减运算),使用arraylist存储符号和数字:第一遍循环只处理乘除运算;第二遍循环处理剩下的加减运算。
示意图如下:
代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList array = new ArrayList<String>();
String input = "";
for (int j = 0; j < n; j++) {
input = in.next();
array.clear();
for (int i = 0; i < input.length(); i++) {
array.add(input.substring(i, i + 1));
}
for (int i = 0; i < array.size(); i++) {
if (array.get(i).equals("x")) { //如果是乘号
int sum = 0;
sum = Integer.parseInt(array.get(i - 1) + "") * Integer.parseInt(array.get(i + 1) + "");
array.remove(i); //先删除乘号
array.add(i, String.valueOf(sum)); //将所得运算结果保存到删除乘号的位置
array.remove(i + 1); //删除乘号后一位运算数字
array.remove(i - 1); //删除乘号前一位运算数字
i--;
} else if (array.get(i).equals("/")) {
int sum = 0;
sum = Integer.parseInt(array.get(i - 1) + "") / Integer.parseInt(array.get(i + 1) + "");
array.remove(i);
array.add(i, String.valueOf(sum));
array.remove(i + 1);
array.remove(i - 1);
i--;
}
}
for (int i = 0; i < array.size(); i++) {
if (array.get(i).equals("+")) {
int sum = 0;
sum = Integer.parseInt(array.get(i - 1) + "") + Integer.parseInt(array.get(i + 1) + "");
array.remove(i);
array.add(i, String.valueOf(sum));
array.remove(i + 1);
array.remove(i - 1);
i--;
}
if (array.get(i).equals("-")) {
int sum = 0;
sum = Integer.parseInt(array.get(i - 1) + "") - Integer.parseInt(array.get(i + 1) + "");
array.remove(i);
array.add(i, String.valueOf(sum));
array.remove(i + 1);
array.remove(i - 1);
i--;
}
}
if (array.get(0).equals("24")) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}