Table Calculation of Question 5 of Group B of the 6th Blue Bridge Cup Final

Topic Description
Title: Table Calculation In
a boredom, atm found a very old program. The function of this program is similar to Excel, it operates on a table.
Assume that the table has n rows, and each row has m cells.
The content of each grid can be a positive integer or a formula.
There are three formulas:
1. SUM(x1,y1:x2,y2) means to find the sum of the values ​​of all the grids in the rectangle where the upper left corner is the x1th row y1 grid, and the lower right corner is the x2 row y2th grid.
2. AVG(x1,y1:x2,y2) means to find the average of the values ​​of all the grids in the rectangle where the upper left corner is the x1th row y1 grid, and the lower right corner is the x2 row y2 grid.
3. STD(x1,y1:x2,y2) means to find the standard deviation of the values ​​of all the grids in the rectangle where the upper left corner is the x1th row y1th grid, and the lower right corner is the x2th row y2th grid.

The standard deviation is the square root of the variance.
Variance is the average of the squares of the differences between each datum and the mean, and is used to measure how far a single datum deviates from the mean.

The formulas are not nested.

If the grid is a number, the value of the grid is equal to the number, otherwise the value of the grid is equal to the result of the grid formula evaluation.

After entering this table, the program will output the value of each grid. ATM thinks this program is very interesting, and he also wants to implement this program.

"Input format"
two numbers n, m in the first line.
Enter a form for the next n lines. Each line consists of m strings separated by spaces, which respectively represent the contents of the corresponding grid.
The input guarantees that there will be no circular dependencies, ie there will be no two lattices a and b such that the value of a depends on the value of b and the value of b depends on the value of a.

"Output Format"
outputs a table with n lines in total, each line contains m real numbers with two decimal places.
The data guarantees that no cell has a value greater than 1e6.

"Sample input"
3 2
1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)

"Sample output"
1.00 5.00
2.00 3.00
3.00 1.48

"Data Range"
For 30% of the data, satisfy: n, m <= 5
For 100% of the data, satisfy: n, m <= 50

Resource convention:
peak memory consumption (including virtual machine) < 512M
CPU consumption < 2000ms

Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

All code is placed in the same source file, after debugging, copy and submit the source code.
Note: Do not use the package statement. Do not use features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

Problem- solving ideas
The biggest difficulty of this problem is that the calculation order of each cell in the table calculation is unknown, and some cell calculations with functions have preconditions, so the solution I adopted is:
use a judgment matrix to record Whether the value of the corresponding table in the table matrix has been calculated, a variable rue that calculates the total number of records in the table matrix determines whether the calculation is complete.
Use a recursive function to calculate the table matrix. If the corresponding position in the judgment matrix is ​​not calculated, it is calculated. If the cell can calculate the value, then rue+1, and then recurse to the next cell for calculation; if the cell cannot calculate the value, skip the calculation of the next cell value, and loop recursively until rue= When n*m, the calculation is completed and the calculation is exited.
java code

import java.util.Scanner;

public class Main5 {
    static String M[][]=new String[51][51];//表格矩阵 - 用于存放表格数据
    static boolean R[][]=new boolean[51][51];//判断矩阵 - 用于判断表格矩阵对应位置的值是否已计算
    static int n,m;//表格矩阵的行,列

    public static double SUM(String X1,String Y1,String X2,String Y2){
        double re=0;
        int x1=Integer.parseInt(X1),x2=Integer.parseInt(X2),y1=Integer.parseInt(Y1),y2=Integer.parseInt(Y2);
        for(int i=x1;i<=x2;i++)
            for(int j=y1;j<=y2;j++)
                re+=Double.parseDouble((M[i][j]));  
        return re;
    }

    public static double AVG(String X1,String Y1,String X2,String Y2){
        return SUM(X1,Y1,X2,Y2)/((Integer.parseInt(Y2)+1-Integer.parseInt(Y1))*(Integer.parseInt(X2)+1-Integer.parseInt(X1)));
    }

    public static double STD(String X1,String Y1,String X2,String Y2){
        double re=0,x=AVG(X1,Y1,X2,Y2);
        int x1=Integer.parseInt(X1),x2=Integer.parseInt(X2),y1=Integer.parseInt(Y1),y2=Integer.parseInt(Y2);
        for(int i=x1;i<=x2;i++)
            for(int j=y1;j<=y2;j++)
                re+=Math.pow((Double.parseDouble(M[i][j])-x), 2);
        return Math.sqrt(re/((y2+1-y1)*(x2+1-x1)));
    }   

    public static void mutil(int rue,int x,int y)//递归计算表格矩阵各个单元格的值
    {
        if(rue==(n*m))//若已计算表格矩阵所有单元格的值,结束递归计算
            return;
        if(!R[x][y])//若表格矩阵的该位置单元格还未计算
        {
            String temp[]=M[x][y].split("\\(|,|:|\\)");//分割单元格内语句
            if(temp[0].equals("SUM"))//若为求和函数
            {
                try{
                    M[x][y]=SUM(temp[1],temp[2],temp[3],temp[4])+"";//调用求和函数
                    ex1(rue,x,y);
                }catch(Exception e){ex2(rue,x,y);}
            }
            else if(temp[0].equals("AVG"))//若为求平均值函数
            {
                try{
                    M[x][y]=AVG(temp[1],temp[2],temp[3],temp[4])+"";//调用求平均值函数
                    ex1(rue,x,y);
                }catch(Exception e){ex2(rue,x,y);}
            }
            else if(temp[0].equals("STD"))//若为求标准差函数
            {
                try{
                    M[x][y]=STD(temp[1],temp[2],temp[3],temp[4])+"";//调用求标准差函数
                    ex1(rue,x,y);
                }catch(Exception e){ex2(rue,x,y);}
            }
            else//若为单独的数值
                ex1(rue,x,y);
        }//end if(!R[x][y])
        else
            ex2(rue,x,y);
    }

    public static void ex1(int rue,int x,int y)//能计算正确情况下(即x1,y1:x2,y2对应矩阵已全部转为数值,没有函数),递归调用mutil函数计算一下个单元格的值,并将已计算单元格数+1
    {
        R[x][y]=true;
        if(x==n&&y==m)
            mutil(rue+1,1,1);
        else if(y==m)
            mutil(rue+1,x+1,1);
        else
            mutil(rue+1,x,y+1);
    }

    public static void ex2(int rue,int x,int y)//无法计算情况下(即x1,y1:x2,y2对应矩阵中还存在函数未计算),递归调用mutil函数计算一下个单元格的值,已计算单元格数不变
    {
        if(x==n&&y==m)
            mutil(rue,1,1);
        else if(y==m)
            mutil(rue,x+1,1);
        else
            mutil(rue,x,y+1);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan=new Scanner(System.in);
        n=scan.nextInt();m=scan.nextInt();//输入行,列
        for(int i=1;i<=n;i++)//输入表格矩阵
            for(int j=1;j<=m;j++)
                M[i][j]=scan.next();

        for(int i=1;i<=n;i++)//初始化判断矩阵
            for(int j=1;j<=m;j++)
                R[i][j]=false;

        mutil(0,1,1);//递归计算
        for(int i=1;i<=n;i++)//输出结果
        {
            for(int j=1;j<=m;j++)
                System.out.printf("%.2f ",Double.parseDouble(M[i][j]));
            System.out.println();
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992139&siteId=291194637
Recommended