📄 3926122_ac_375ms_2204k.java
字号:
import java.util.*;
public class Main {
private Scanner in;
private String[][] sheet;
public static void main(String[] args) {
new Main().run();
}
private int calc(int i, int j) {
if (sheet[i][j].charAt(0) != '=') {
return Integer.parseInt(sheet[i][j]);
}
int ret = 0;
StringTokenizer st = new StringTokenizer(
sheet[i][j].substring(1), "+");
while (st.hasMoreTokens()) {
String pos = st.nextToken();
int p;
for (p = 0; p < pos.length(); p++) {
if (Character.isDigit(pos.charAt(p))) {
break;
}
}
ret += calc(Integer.parseInt(pos.substring(p)) - 1,
getCol(pos.substring(0, p)) - 1);
}
sheet[i][j] = Integer.toString(ret);
return ret;
}
private int getCol(String str) {
if (str.length() == 1) {
return str.charAt(0) - 'A' + 1;
}
if (str.length() == 2) {
return (str.charAt(0) - 'A' + 1) * 26 + str.charAt(1) - 'A' + 1;
}
return (str.charAt(0) - 'A' + 1) * 26 * 26
+ (str.charAt(1) - 'A' + 1) * 26
+ str.charAt(2) - 'A' + 1;
}
private void run() {
in = new Scanner(System.in);
int row, col;
for (int cas = in.nextInt(); cas != 0; cas--) {
col = in.nextInt();
row = in.nextInt();
sheet = new String[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
sheet[i][j] = in.next();
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
try {
System.out.print(calc(i, j));
} catch (Exception e) {
e.printStackTrace();
}
System.out.print(
j != col - 1 ? " " : "\n");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -