2015蓝桥杯决赛第五题 表格计算

某次无聊中, 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.util.Arrays;
import java.util.Scanner;
class Main
{
	static double mat[][];
	static int n,m;
	static int x[][];
	static double getvalue(int index)
	{
		double t;
		if(mat[index/m][index%m]==1e10)
			{
			t=f(index)*100;
			if(t-(int)t>=0.5)
				t++;
			return mat[index/m][index%m]=(int)t/100.0;
			}
		return mat[index/m][index%m];
	}
	static double f(int index)
	{
		if(x[index][0]==1)
		{
			double sum=0;
			for(int i=x[index][1];i<=x[index][3];i++)
				for(int j=x[index][2];j<=x[index][4];j++)
					sum+=getvalue(i*m+j);
			return sum;
		}
		else if(x[index][0]==2)
		{
			double sum=0,avg=0;
			int t=0;
			for(int i=x[index][1];i<=x[index][3];i++)
				for(int j=x[index][2];j<=x[index][4];j++)
					{sum+=getvalue(i*m+j);t++;}
			avg=sum/t;	
			return avg;
		}
		else 
		{
			double sum=0,avg=0,var=0;
			int t=0;
			for(int i=x[index][1];i<=x[index][3];i++)
				for(int j=x[index][2];j<=x[index][4];j++)
					{sum+=getvalue(i*m+j);t++;}
			avg=sum/t;
			for(int i=x[index][1];i<=x[index][3];i++)
				for(int j=x[index][2];j<=x[index][4];j++)
					var+=Math.pow(getvalue(i*m+j)-avg, 2)/t;		
			return Math.sqrt(var);
		}
		
	}
	public static void main(String args[])
	{
		Scanner scanner=new Scanner(System.in);
		n=scanner.nextInt();
		m=scanner.nextInt();
		mat=new double[n][m];
		x=new int[n*m][5];
		String string;
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				mat[i][j]=1e10;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				string=scanner.next();
				if(string.length()<=3)
					mat[i][j]=Double.valueOf(string);
				else if(string.substring(0, 3).equals("SUM"))
				{
					x[i*m+j][0]=1;
					x[i*m+j][1]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[0])-1;
					x[i*m+j][3]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[0])-1;
					x[i*m+j][2]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[1])-1;
					x[i*m+j][4]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[1])-1;
}
				else if(string.substring(0, 3).equals("AVG"))
				{
					x[i*m+j][0]=2;
					x[i*m+j][1]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[0])-1;
					x[i*m+j][3]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[0])-1;
					x[i*m+j][2]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[1])-1;
					x[i*m+j][4]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[1])-1;
}
				else if(string.substring(0, 3).equals("STD"))
				{
					x[i*m+j][0]=3;
					x[i*m+j][1]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[0])-1;
					x[i*m+j][3]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[0])-1;
					x[i*m+j][2]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[0].split(",")[1])-1;
					x[i*m+j][4]=Integer.valueOf(string.substring(4,string.length()-1).split(":")[1].split(",")[1])-1;
				}
				else mat[i][j]=Double.valueOf(string);
			}
		}
		for(int i=0;i<n;i++)
			{
			for(int j=0;j<m;j++)
				System.out.printf("%.2f ",getvalue(i*m+j));
		System.out.println();

			}
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值