题目描述
题目内容来自(https://blog.csdn.net/goldarmour/article/details/129152400)
【Excel单元格数值统计】
Excel 工作表中对选定区域的数值进行统计的功能非常实用。
仿照Excel的这个功能,请对给定表格中选中区域中的单元格进行求和统计,并输出统计结果。
为简化计算,假设当前输入中每个单元格内容仅为数字或公式两种。
● 如果为数字,则是一个非负整数,形如3、77
● 如果为公式,则固定以=开头,且仅包含下面三种情况:
1.等于某单元格的值,例如=B12
2.两个单元格的双目运算(仅为+或-),形如=C1-C2、C3+B2
3.单元格和数字的双目运算(仅为+或-),形如=B1+1、100-B2
注意:
公式内容都是合法的,例如不存在,=C+1、=C1-C2+B3,=5、=3+5
不存在循环引用,例如A1=B1+C1、C1=A1+B2
内容中不存在空格、括号
输入描述:
第一行两个整数rows cols,表示给定表格区域的行数和列数,1<=rows<=20,1<=cols<=26。
接下来rows行,每行cols个以空格分隔的字符串,表示给定表格values的单元格内容。
最后一行输入的字符串,表示给定的选中区域,形如A1:C2。
输出描述:
一个整数,表示给定选中区域各单元格中数字的累加总和,范围-2,147,483,648 ~ 2,147,483,647
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
1 3
1 =A1+C1 3
A1:C1
输出
8
示例2 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
5 3
10 12 =C5
15 5 6
7 8 =3+C2
6 =B2-A1 =C2
7 5 3
B2:C4
输出
29
备注:
表格的行号1~20,列号A~Z,例如单元格B3对应values[2][1]。
一种解法
不保证通过率
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* https://blog.csdn.net/goldarmour/article/details/129152400
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
int r = Integer.parseInt(split[0]);
int c = Integer.parseInt(split[1]);
int[][] nums = new int[r][c];
String[][] shi = new String[r][c];
for (int i = 0; i < r; i++) {
String[] strings = br.readLine().split(" ");
for (int j = 0; j < c; j++) {
if (strings[j].startsWith("=")) {
shi[i][j] = strings[j];
} else {
shi[i][j] = "";
nums[i][j] = Integer.parseInt(strings[j]);
}
}
}
String[] need = br.readLine().split(":");
int nx1,nx2,ny1,ny2;
nx1 = need[0].charAt(0)-'A';
nx2 = need[1].charAt(0)-'A';
ny1 = Integer.parseInt(need[0].substring(1))-1;
ny2 = Integer.parseInt(need[1].substring(1))-1;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
String s = shi[i][j];
if (!s.isEmpty()) {
s = s.substring(1);
int x1, x2, y1, y2;
int num1, num2;
if (s.contains("+")) {
int t = s.indexOf('+');
if (s.charAt(0) >= 'A') {
x1 = s.charAt(0) - 'A';
y1 = Integer.parseInt(s.substring(1, t))-1;
num1 = nums[y1][x1];
} else {
num1 = Integer.parseInt(s.substring(0, t));
}
if (s.charAt(t + 1) >= 'A') {
x2 = s.charAt(t + 1) - 'A';
y2 = Integer.parseInt(s.substring(t + 2))-1;
num2 = nums[y2][x2];
} else {
num2 = Integer.parseInt(s.substring(0, t));
}
nums[i][j] = num1 + num2;
} else if (s.contains("-")) {
int t = s.indexOf('-');
if (s.charAt(0) >= 'A') {
x1 = s.charAt(0) - 'A';
y1 = Integer.parseInt(s.substring(1, t))-1;
num1 = nums[y1][x1];
} else {
num1 = Integer.parseInt(s.substring(0, t));
}
if (s.charAt(t + 1) >= 'A') {
x2 = s.charAt(t + 1) - 'A';
y2 = Integer.parseInt(s.substring(t + 2))-1;
num2 = nums[y2][x2];
} else {
num2 = Integer.parseInt(s.substring(0, t));
}
nums[i][j] = num1 - num2;
} else {
int x = s.charAt(0) - 'A';//lie
int y = Integer.parseInt(s.substring(1))-1;//hang
nums[i][j] = nums[y][x];
}
}
}
}
int result = 0;
for (int i = ny1; i <= ny2; i++) {
for (int j = nx1; j <= nx2; j++) {
result+=nums[i][j];
}
}
System.out.println(result);
}
}