题目:
某次无聊中, atm 发现了一个很老的程序。这个程序的功能类似于 Excel ,它对一个表格进行操作。
不妨设表格有 n 行,每行有 m 个格子。
每个格子的内容可以是一个正整数,也可以是一个公式。
公式包括三种:
1. SUM(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的和。
2. AVG(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的平均数。
3. STD(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的标准差。标准差即为方差的平方根。
方差就是:每个数据与平均值的差的平方的平均值,用来衡量单个数据离开平均数的程度。公式都不会出现嵌套。
如果这个格子内是一个数,则这个格子的值等于这个数,否则这个格子的值等于格子公式求值结果。
输入这个表格后,程序会输出每个格子的值。atm 觉得这个程序很好玩,他也想实现一下这个程序。
「输入格式」
第一行两个数 n, m 。
接下来 n 行输入一个表格。每行 m 个由空格隔开的字符串,分别表示对应格子的内容。
输入保证不会出现循环依赖的情况,即不会出现两个格子 a 和 b 使得 a 的值依赖 b 的值且 b 的值依赖 a 的值。「输出格式」
输出一个表格,共 n 行,每行 m 个保留两位小数的实数。
数据保证不会有格子的值超过 1e6 。「样例输入」
3 2
1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)「样例输出」
1.00 5.00
2.00 3.00
3.00 1.48
思路:
首先要将这n行m列的数据用String类型二维数组存放起来,然后我们仍然需要用一个二维数组存放已得出数字,并且需要标记它是否为已得出,因此用boolean值来标记。
代码:
public class LQB6biaogetianshu {
public static String[][] arr;
public static double[][] number;
public static boolean[][] isNumber;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
arr = new String[n+1][m+1];
isNumber = new boolean[n+1][m+1];
number = new double[n+1][m+1];
for(int i = 1;i <= n; i++){
for(int j = 1; j <= m; j++){
arr[i][j] = sc.next();
int temp = arr[i][j].charAt(0);
if(temp >= 48 && temp <= 57){
isNumber[i][j] = true;
number[i][j] = Double.valueOf(arr[i][j]);
}
}
}
for(int i = 1;i <= n; i++){
for(int j = 1; j <= m; j++){
if(!isNumber[i][j])
fun(arr[i][j],i,j);
}
}
DecimalFormat format = new DecimalFormat("0.00");
for(int i = 1;i <= n; i++){
for(int j = 1; j <= m; j++){
System.out.print(format.format(number[i][j])+" ");
}
System.out.println();
}
}
private static Double fun(String arr,int i,int j) {
if(arr.startsWith("SUM")){
if(isNumber[i][j]){
return number[i][j];
}else{
number[i][j] = sum(arr.charAt(4)-48,arr.charAt(6)-48,arr.charAt(8)-48,arr.charAt(10)-48);
isNumber[i][j] = !isNumber[i][j];
return number[i][j];
}
}else if(arr.startsWith("AVG")){
if(isNumber[i][j]){
return number[i][j];
}else{
number[i][j] = avg(arr.charAt(4)-48,arr.charAt(6)-48,arr.charAt(8)-48,arr.charAt(10)-48);
isNumber[i][j] = !isNumber[i][j];
return number[i][j];
}
}else{
if(isNumber[i][j]){
return number[i][j];
}else{
number[i][j] = std(arr.charAt(4)-48,arr.charAt(6)-48,arr.charAt(8)-48,arr.charAt(10)-48);
isNumber[i][j] = !isNumber[i][j];
return number[i][j];
}
}
}
private static double std(int x1, int y1, int x2, int y2) {
// TODO Auto-generated method stub
double avg = avg(x1,y1,x2,y2);
double temp = 0;int count = 0;
for(int i = x1;i <= x2; i++){
for(int j = y1; j <= y2; j++){
temp += Math.pow((fun(arr[i][j],i,j) - avg), 2);
count++;
}
}
return Math.sqrt(temp/count);
}
private static double avg(int x1, int y1, int x2, int y2) {
// TODO Auto-generated method stub
double temp = 0;int count = 0;
for(int i = x1;i <= x2; i++){
for(int j = y1; j <= y2; j++){
temp += fun(arr[i][j],i,j);
count++;
}
}
return temp/count;
}
private static double sum(int x1, int y1, int x2, int y2) {
// TODO Auto-generated method stub
double temp = 0;
for(int i = x1;i <= x2; i++){
for(int j = y1; j <= y2; j++){
if(isNumber[i][j]){
temp += number[i][j];
}else{
temp += fun(arr[i][j],i,j);
}
}
}
return temp;
}
}