个人代码,如果大家有更好的优化意见可以留言
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;
}
}