表格计算
某次无聊中, 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
数据范围
对于 30% 的数据,满足: n, m <= 5
对于 100% 的数据,满足: n, m <= 50
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
利用拓扑排序进行模拟:
import java.text.DecimalFormat;
import java.util.*;
public class Main {
static int maxn = 50+5;
static boolean[] flag = new boolean[maxn*maxn+10];//标识当前是否已经是数字,也就是是否已经计算出结果
static int[] inDegree = new int[maxn*maxn+10];//入度
static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
static int[][] contact = new int[maxn*maxn][maxn*maxn];
static int n = 0, m = 0;
static Scanner in = new Scanner(System.in);
static String[] mp = new String[maxn*maxn+10];//no error
static double[] val = new double[maxn*maxn+10];
public static void main(String[] args) {
n = in.nextInt();
m = in.nextInt();
Arrays.fill(val, 0.0);
for (int i = 0; i < maxn*maxn; i++) {
for (int j = 0; j < maxn*maxn; j++) {
contact[i][j] = 0;
}
}
int ans = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
String cas = in.next();
//计算当前编号
int id = (i - 1)*m + j;
mp[id] = cas;
//如果这个不是数字,那么需要计算入度
if (cas.startsWith("SUM") || cas.startsWith("AVG") || cas.startsWith("STD")) {
int area = 0;
int x1 = cas.charAt(4) - '0', y1 = cas.charAt(6) - '0', x2 = cas.charAt(8) - '0', y2 = cas.charAt(10) - '0';
int width = Math.abs(x1 - x2) + 1;
int height = Math.abs(y1 - y2) + 1;
for (int p = x1; p <= x2; p++) {
for (int k = y1; k <= y2; k++) {
int cid = (p - 1)*m + k;
contact[cid][id] = 1;
}
}
inDegree[id] = width*height;
flag[id] = false;
}//如果是数字入度就为0
else {
flag[id] = true;
inDegree[id] = 0;
val[id] = Double.valueOf(cas);
}
}
}
topollogicalSort();
ouput();
}
private static void ouput() {
DecimalFormat format = new DecimalFormat("0.00");
for (int i = 1; i <= n*m; i++) {
if (i%m == 0) {
System.out.println(format.format(val[i]));
}
else {
System.out.print(format.format(val[i]) + " ");
}
}
}
public static void topollogicalSort() {
Queue<Integer> que = new LinkedList<Integer>();
//把入度为0先全部加入队列
for (int i = 1; i <= n*m; i++) {
if (flag[i] == true) {
que.add(i);
}
}
while (!que.isEmpty()) {
int u = que.peek(); que.poll();
//遍历整个图
//遍历当前点的所有出边,然后所有出边的inDegree--;
//if inDegree == 0, add que
for (int i = 1; i <= n*m; i++) {
if (contact[u][i] == 1 && inDegree[i] > 0 && flag[i] == false) {
inDegree[i]--;
if (inDegree[i] == 0) {
double new_val = cal(i);
flag[i] = true;
que.add(i);
}
}
}
//清空关系
for (int i = 1; i <= n*m; i++) {
contact[u][i] = 0;
}
}
}
private static double cal(int id) {
if (!(mp[id].startsWith("SUM") || mp[id].startsWith("AVG") || mp[id].startsWith("STD"))) {
return 0.0;
}
String cas = mp[id];
int x1 = cas.charAt(4) - '0', y1 = cas.charAt(6) - '0', x2 = cas.charAt(8) - '0', y2 = cas.charAt(10) - '0';
int w = Math.abs(x1 - x2) + 1;
int h = Math.abs(y1 - y2) + 1;
double all = w*h*1.0;
double sum = 0.0;
for (int k = x1; k <= x2; k++) {
for (int p = y1; p <= y2; p++) {
int cid = (k - 1)*m + p;
sum += val[cid];
}
}
double avg = sum*1.0/all;
if (mp[id].startsWith("SUM")) {
val[id] = sum;
return sum;
}
else if (mp[id].startsWith("AVG")) {
val[id] = avg;
return avg;
}
else if (mp[id].startsWith("STD")) {
double sum2 = 0.0;
for (int k = x1; k <= x2; k++) {
for (int p = y1; p <= y2; p++) {
int cid = (k - 1)*m + p;
sum2 += (val[cid] - avg)*1.0*(val[cid] - avg)*1.0;
}
}
double avg2 = Math.sqrt(sum2*1.0/all);
val[id] = avg2;
return avg2;
}
return 0.0;
}
}