已知火星人使用的运算符号为#
;$
其与地球人的等价公式如下
x#y=4*x+3*y+2
x$y=2*x+y+3
x
y
是无符号整数
地球人公式按照c语言规则进行计算
火星人公式中#
符优先级高于$
相同的运算符按从左到右的顺序运算
火星人字符串表达式结尾不带回车换行
输入的字符串说明是 字符串为仅有无符号整数和操作符组成的计算表达式
123#4$5#76$78
#4$5
这种缺少操作数4$5#
这种缺少操作数4#$5
这种缺少操作数4 $5
有空格3+4-5*6/7
有其他操作符12345678987654321$54321
32位整数溢出根据火星人字符串输出计算结果
结尾不带回车换行
7#6$5#12
157
7#6$5#12=(4*7+3*6+2)$5#12
=48$5#12
=48$(4*5+3*12+2)
=48$58
=2*48+58+3
=157
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2022/7/16
* Time: 9:35
* Description:
*/
public class Main0103 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String input = scanner.nextLine();
solution(input);
}
}
private static void solution(String input) {
List<String> operators = Arrays.stream(input.split("\\w+"))
.filter(x -> !x.isEmpty())
.collect(Collectors.toList());
List<Integer> nums = Arrays.stream(input.split("\\W+"))
.map(Integer::parseInt)
.collect(Collectors.toList());
int posS = operators.indexOf("#");
while (posS != -1) {
int tmp = sharp(nums.get(posS), nums.get(posS + 1));
nums.set(posS, tmp);
nums.remove(posS + 1);
operators.remove(posS);
posS = operators.indexOf("#");
}
int res = nums.get(0);
for (int i = 1; i < nums.size(); i++) {
res = dollar(res, nums.get(i));
}
System.out.println(res);
}
public static int sharp(int x, int y) {
return 4 * x + 3 * y + 2;
}
public static int dollar(int x, int y) {
return 2 * x + y + 3;
}
}