P1957
测试点状态
import java.util.Scanner;
import java.util.function.BinaryOperator;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
scanner.nextLine();
char lastOperation = ' ';
int lastResult = 0;
Map<Character, BinaryOperator<Integer>> operations = new HashMap<>();
operations.put('a', (x, y) -> x + y);
operations.put('b', (x, y) -> x - y);
operations.put('c', (x, y) -> x * y);
for (int j = 0; j < i; j++) {
String[] input = scanner.nextLine().split(" ");
char operation;
int number1, number2;
if (input.length == 3) {
operation = input[0].charAt(0);
number1 = Integer.parseInt(input[1]);
number2 = Integer.parseInt(input[2]);
} else {
operation = lastOperation;
number1 = lastResult;
number2 = Integer.parseInt(input[0]);
}
int result = operations.getOrDefault(operation, (x, y) -> {
throw new IllegalArgumentException("Invalid operation.");
}).apply(number1, number2);
String expression = number1 + (operation == 'a' ? "+" : operation == 'b' ? "-" : "*") + number2 + "=" + result;
int length = expression.length();
System.out.println(expression);
System.out.println(length);
lastOperation = operation;
lastResult = result;
}
scanner.close();
}
}