蓝桥杯 表格计算

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
double res[100][100];
string str[100][100];
int flag[100][100];
double sum(int,int,int,int);
double avg(int,int,int,int);
double stdd(int,int,int,int);
double func(int i,int j){
	int x1,x2,y1,y2;//这么转换string型我也很恶心
	int pos=0;
	if(str[i][j][0]=='S'||str[i][j][0]=='A'){
		if(str[i][j][5]>='0'&&str[i][j][5]<='9'){
			x1=(str[i][j][4]-'0')*10+str[i][j][5]-'0';
			pos+=1; 
		}
		else{
			x1=str[i][j][4]-'0';
		}
		if(str[i][j][7+pos]>='0'&&str[i][j][7+pos]<='9'){
			y1=(str[i][j][6+pos]-'0')*10+str[i][j][7+pos]-'0';
			pos+=1;
		}
		else{
			y1=str[i][j][6+pos]-'0';
		}
		if(str[i][j][9+pos]>='0'&&str[i][j][9+pos]<='9'){
			x2=(str[i][j][8+pos]-'0')*10+str[i][j][9+pos]-'0';
			pos+=1;
		}
		else{
			x2=str[i][j][8+pos]-'0';
		}
		if(str[i][j][11+pos]>='0'&&str[i][j][11+pos]<='9'){
			y2=(str[i][j][10+pos]-'0')*10+str[i][j][11+pos]-'0';
			pos+=1; 
		}
		else{
			y2=str[i][j][10+pos]-'0';
		}
	}
	if(res[i][j]!=0||flag[i][j]) return res[i][j];
	else if(str[i][j][0]=='S'&&str[i][j][1]=='U'){
		res[i][j]=sum(x1,y1,x2,y2);
		flag[i][j]=1;
	}
	else if(str[i][j][0]=='S'&&str[i][j][1]=='T'){
		res[i][j]=stdd(x1,y1,x2,y2);
		flag[i][j]=1;
	}
	else if(str[i][j][0]=='A'){
		res[i][j]=avg(x1,y1,x2,y2);//!!!
		flag[i][j]=1;
	}
	return res[i][j];
}
int main(){
	int n,m;
	cin>>n>>m;
	getchar();
	memset(res,0,sizeof(res));
	memset(flag,0,sizeof(flag));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>str[i][j];
			if(str[i][j][0]>='0'&&str[i][j][0]<='9'){
				flag[i][j]=1;
				int tt=str[i][j].length()-1,t=1;
				while(tt>=0){
					res[i][j]+=(str[i][j][tt]-'0')*t;
					t*=10;
					tt--;
				}
			}
		}
	} 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(res[i][j]==0){
				res[i][j]=func(i,j);
			} 
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(j==m){
				printf("%.2lf",res[i][j]);
			}
			else{
				printf("%.2lf ",res[i][j]);
			}
		}
		printf("\n");
	}
	return 0;
} 
double avg(int x1,int y1,int x2,int y2){
	double summ=0,temp;
	int cnt=0;
	for(int i=x1;i<=x2;i++){
		for(int j=y1;j<=y2;j++){
			temp=func(i,j);
			summ+=temp;
			cnt++;
		}
	}
	return summ/(cnt*1.0);
}
double sum(int x1,int y1,int x2,int y2){
	double summ=0,temp;
	for(int i=x1;i<=x2;i++){
		for(int j=y1;j<=y2;j++){
			temp=func(i,j);
			summ+=temp;
		}
	}
	return summ;
}
double stdd(int x1,int y1,int x2,int y2){
	double summ=0,temp;
	int cnt=0;
	for(int i=x1;i<=x2;i++){
		for(int j=y1;j<=y2;j++){
			temp=func(i,j);
			summ+=temp;
			cnt++;
		}
	}
	double temp2=summ/cnt*(1.0);
	summ=0;
	for(int i=x1;i<=x2;i++){
		for(int j=y1;j<=y2;j++){
			temp=func(i,j);
			temp=abs(temp-temp2);
			temp*=temp;
			summ+=temp;
		}
	}
	return sqrt(summ/(cnt*1.0));
}

猜你喜欢

转载自blog.csdn.net/chan_yeol/article/details/68933826
今日推荐