2015年蓝桥杯JavaB组表格计算

个人代码,如果大家有更好的优化意见可以留言

import java.util.Scanner;




public class Main2 {
public static int indexN =0;
public static int indexM =0;
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
String[][] arr= new String[n][m];

for(int i=0;i<arr.length;i++){
for(int j =0;j<arr[i].length;j++){
arr[i][j] =sc.next();
}
}
displayAll(arr);


}




public static void displayAll(String[][] arr){


if(indexN==arr.length){

}else{
if(indexM<arr[indexN].length){
if(arr[indexN][indexM].contains("SUM")){
int x1=Integer.parseInt(arr[indexN][indexM].substring(4, 5));
int y1=Integer.parseInt(arr[indexN][indexM].substring(6, 7));
int x2=Integer.parseInt(arr[indexN][indexM].substring(8, 9));
int y2=Integer.parseInt(arr[indexN][indexM].substring(10, 11));
System.out.print(SUM(x1,y1,x2,y2,arr)+" ");

}else if(arr[indexN][indexM].contains("AVG")){
int x1=Integer.parseInt(arr[indexN][indexM].substring(4, 5));
int y1=Integer.parseInt(arr[indexN][indexM].substring(6, 7));
int x2=Integer.parseInt(arr[indexN][indexM].substring(8, 9));
int y2=Integer.parseInt(arr[indexN][indexM].substring(10, 11));
System.out.print(AVG(x1,y1,x2,y2,arr)+" ");
}else if(arr[indexN][indexM].contains("STD")){
int x1=Integer.parseInt(arr[indexN][indexM].substring(4, 5));
int y1=Integer.parseInt(arr[indexN][indexM].substring(6, 7));
int x2=Integer.parseInt(arr[indexN][indexM].substring(8, 9));
int y2=Integer.parseInt(arr[indexN][indexM].substring(10, 11));
System.out.print(STD(x1,y1,x2,y2,arr)+" ");
}
else{
System.out.print(Float.parseFloat(arr[indexN][indexM])+" ");
}
indexM++;
displayAll(arr);
}else{
System.out.println();
indexN++;
indexM=0;
displayAll(arr);
}
}


}


public static float AVG(int x1,int y1,int x2,int y2,String[][] arr){
int index =0;
float avg = 0;
for(int i=x1-1;i<=x2-1;i++){
for(int j =y1-1;j<=y2-1;j++){
index++;
if(arr[i][j].contains("SUM")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
avg+=SUM(x3,y3,x4,y4,arr);
}else if(arr[i][j].contains("AVG")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
avg+=AVG(x3,y3,x4,y4,arr);
}else if(arr[i][j].contains("STD")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
avg+=STD(x3,y3,x4,y4,arr);
}
else{
avg+=Float.parseFloat(arr[i][j]);
}
}
}
return avg/index;
}


public static float STD(int x1,int y1,int x2,int y2,String[][] arr){
float avg = AVG(x1,y1,x2,y2,arr);
int index =0;
float std = 0;
for(int i=x1-1;i<=x2-1;i++){
for(int j =y1-1;j<=y2-1;j++){
index++;
if(arr[i][j].contains("SUM")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
std+=(float) Math.pow((SUM(x3,y3,x4,y4,arr)-avg),2);

}else if(arr[i][j].contains("AVG")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
std+=(float) Math.pow((AVG(x3,y3,x4,y4,arr)-avg),2);

}else if(arr[i][j].contains("STD")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
std+=(float) Math.pow((STD(x3,y3,x4,y4,arr)-avg),2);
}
else{

std+=(float) Math.pow((Float.parseFloat(arr[i][j])-avg),2);
}
}
}
return std/index;
}


public static float SUM(int x1,int y1,int x2,int y2,String[][] arr){
float sum = 0;
for(int i=x1-1;i<=x2-1;i++){
for(int j =y1-1;j<=y2-1;j++){
if(arr[i][j].contains("SUM")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
sum+=SUM(x3,y3,x4,y4,arr);
}else if(arr[i][j].contains("AVG")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
sum+=AVG(x3,y3,x4,y4,arr);
}else if(arr[i][j].contains("STD")){
int x3=Integer.parseInt(arr[i][j].substring(4, 5));
int y3=Integer.parseInt(arr[i][j].substring(6, 7));
int x4=Integer.parseInt(arr[i][j].substring(8, 9));
int y4=Integer.parseInt(arr[i][j].substring(10, 11));
sum+=STD(x3,y3,x4,y4,arr);
}
else{
sum+=Float.parseFloat(arr[i][j]);
}
}
}
return sum;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值