■ 题目描述
【火星文计算】
已知火星人使用的运算符为#、$,其与地球人的等价公式如下:
x#y = 2*x+3*y+4
x$y = 3*x+y+2
1、其中x、y是无符号整数
2、地球人公式按C语言规则计算
3、火星人公式中,$的优先级高于#,相同的运算符,按从左到右的顺序计算
现有一段火星人的字符串报文,请你来翻译并计算结果。
输入描述
火星人字符串表达式(结尾不带回车换行)
输入的字符串说明:字符串为仅由无符号整数和操作符(#、$)组成的计算表达式。
例如:123#4$5#67$78。
- 用例保证字符串中,操作数与操作符之间没有任何分隔符。
- 用例保证操作数取值范围为32位无符号整数。
- 保证输入以及计算结果不会出现整型溢出。
- 保证输入的字符串为合法的求值报文,例如:123#4$5#67$78
- 保证不会出现非法的求值报文,例如类似这样字符串:
#4$5 //缺少操作数
4$5# //缺少操作数
4#$5 //缺少操作数
4 $5 //有空格
3+4-5*6/7 //有其它操作符
12345678987654321$54321 //32位整数计算溢出
输出描述
根据输入的火星人字符串输出计算结果(结尾不带回车换行)。
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
7#6$5#12
输出
226
说明
示例
7#6$5#12
=7#(3*6+5+2)#12
=7#25#12
=(2*7+3*25+4)#12
=93#12
=2*93+3*12+4
=226
Java代码实现1:
import java.util.Scanner;
public class MarsTextCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String input = sc.nextLine();
System.out.println(func(input));
}
sc.close();
}
private static int func(String input) {
int index = input.lastIndexOf("#");
if (index != -1) {
String left = input.substring(0, index);
String right = input.substring(index + 1);
return 2 * func(left) + 3 * func(right) + 4;
}
index = input.lastIndexOf("$");
if (index != -1) {
String left = input.substring(0, index);
String right = input.substring(index + 1);
return 3 * func(left) + func(right) + 2;
}
return Integer.parseInt(input);
}
}
Java代码实现2:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
while (input.contains("$") || input.contains("#")) {
if (input.contains("$")) {
int idx = input.lastIndexOf("$");
int leftStart = changeInput(input, idx, -1);
int op1 = Integer.parseInt(input.substring(leftStart, idx));
int rightEnd = changeInput(input, idx, +1);
int op2 = Integer.parseInt(input.substring(idx + 1, rightEnd));
int res = calcStar('$', op1, op2);
input = input.substring(0, leftStart) + res + input.substring(rightEnd);
} else {
if (input.contains("#")) {
int idx = input.indexOf("#", 0);
int leftStart = changeInput(input, idx, -1);
int op1 = Integer.parseInt(input.substring(leftStart, idx));
int rightEnd = changeInput(input, idx, +1);
int op2 = Integer.parseInt(input.substring(idx + 1, rightEnd));
int res = calcStar('#', op1, op2);
input = input.substring(0, leftStart) + res + input.substring(rightEnd);
}
}
}
System.out.println(input);
}
private static int changeInput(String input, int idx, int opt) {
char ch = input.charAt(idx + opt);
while (ch >= '0' && ch <= '9') {
if (opt > 0) {
opt++;
} else {
opt--;
}
if (idx + opt >= 0 && idx + opt < input.length()) {
ch = input.charAt(idx + opt);
} else {
break;
}
}
if (opt < 0) {
return opt + idx + 1;
} else {
return opt + idx;
}
}
private static int calcStar(char ch, int op1, int op2) {
if (ch == '#') {
return op1 * 2 + op2 * 3 + 4;
} else {
return 3 * op1 + op2 + 2;
}
}
}