import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long sum = 0;
String result = null;
Integer K0 = 10;
Scanner cin = new Scanner(System.in);
sum = Long.parseLong(cin.nextLine());
while (sum > 0) {
String s = cin.nextLine();
sum = sum - 1;
if (s.equals("CLEAR")) {
result = null;
} else if (s.equals("ADD")) {
while (true) {
if (sum > 0) {
s = cin.nextLine();
sum--;
if (s.substring(0, 3).equals("NUM")) {
long new_data = Long.parseLong(s.substring(4), K0);
long r = Long.parseLong(result, K0);
r = add(r, new_data);
result = change(r, 10, K0);
break;
} else if (s.equals("CLEAR")) {
result = null;
break;
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
}
} else
break;
}
} else if (s.equals("SUB")) {
while (true) {
if (sum > 0) {
s = cin.nextLine();
sum--;
if (s.substring(0, 3).equals("NUM")) {
long new_data = Long.parseLong(s.substring(4), K0);
long r = Long.parseLong(result, K0);
r = sub(r, new_data);
result = change(r, 10, K0);
break;
} else if (s.equals("CLEAR")) {
result = null;
break;
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
}
} else
break;
}
} else if (s.equals("MUL")) {
while (true) {
if (sum > 0) {
s = cin.nextLine();
sum--;
if (s.substring(0, 3).equals("NUM")) {
long new_data = Long.parseLong(s.substring(4), K0);
long r = Long.parseLong(result, K0);
r = mul(r, new_data);
result = change(r, 10, K0);
break;
} else if (s.equals("CLEAR")) {
result = null;
break;
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
}
} else
break;
}
} else if (s.equals("DIV")) {
while (true) {
if (sum > 0) {
s = cin.nextLine();
sum--;
if (s.substring(0, 3).equals("NUM")) {
long new_data = Long.parseLong(s.substring(4), K0);
long r = Long.parseLong(result, K0);
r = div(r, new_data);
result = change(r, 10, K0);
break;
} else if (s.equals("CLEAR")) {
result = null;
break;
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
}
} else
break;
}
} else if (s.equals("MOD")) {
while (true) {
if (sum > 0) {
s = cin.nextLine();
sum--;
if (s.substring(0, 3).equals("NUM")) {
long new_data = Long.parseLong(s.substring(4), K0);
long r = Long.parseLong(result, K0);
r = mod(r, new_data);
result = change(r, 10, K0);
break;
} else if (s.equals("CLEAR")) {
result = null;
break;
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
}
} else
break;
}
} else if (s.substring(0, 3).compareTo("NUM") == 0) {
result = s.substring(4);
} else if (s.substring(0, 3).equals("CHA")) {
if (result != null) {
long r = Long.parseLong(result, K0);
K0 = Integer.parseInt(s.substring(7));
result = change(r, 10, K0);
} else
K0 = Integer.parseInt(s.substring(7));
} else if (s.equals("EQUAL")) {
System.out.println(result);
}
}
cin.close();
}
public static long add(long a, long b) {
return a + b;
}
public static long sub(long a, long b) {
return a - b;
}
public static long mul(long a, long b) {
return a * b;
}
public static long div(long a, long b) {
return a / b;
}
public static long mod(long a, long b) {
return a % b;
}
public static String change(long a, Integer K0, int K) {
String str = String.valueOf(a);
a = Long.parseLong(str, K0);
str = Long.toString(a, K);
str = str.toUpperCase();
return str;
}
}
进行代码测试,以较高的效率顺利通过各个评测点。