The growth value of SH2018 written test questions

1. The topic

     see code

2. Code

import java.util.Scanner;

/**
 * 3
 * 1 1 5 10
 * 2 3 4
 * 1 4 6 -5
 */
public class main4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            //Enter the number of rows row
            int row = in.nextInt();

            //input growth value
            String[] strArr = new String[row];
            String a = in.nextLine();
            for (int i = 0; i < strArr.length; i++) {
                strArr[i] = in.nextLine();
            }

            //test
            System.out.println(strArr[0]);
            System.out.println(strArr[1]);
            System.out.println(strArr[2]);
            //

            //calculate growth value
            int mLast = 0; // start time of daily growth value
            int nLast = 0; //End time of daily growth value
            int valueLast = 0;//Daily growth value
            int mNext = 0; //Start time of daily growth value
            int nNext = 0; //End time of daily growth value
            int valueNext = 0;//Daily growth value
            int value2Sum = 0; //The cumulative value of task growth value
            int value1Sum = 0; //The cumulative value of daily growth value
            int count = 0; //How many pieces of data are there to count the daily growth value
            int sum = 0; //Total growth value

            for (int i = 0; i < strArr.length; i++) {
                String[] strArr1 = strArr[i].split(" ");

                //test
                for (int j = 0; j < strArr1.length; j++) {
                    System.out.println(strArr1[j]);
                }
                //


                if (Integer.parseInt(strArr1[0]) == 2) {
                    value2Sum = value2Sum + Integer.parseInt(strArr1[2]);
                }


                if (count != 0) {
                    if (Integer.parseInt(strArr1[0]) == 1) {
                        mNext = Integer.parseInt(strArr1[1]);
                        nNext = Integer.parseInt(strArr1[2]);
                        valueNext = Integer.parseInt(strArr1[3]);
                        value1Sum = value1Sum + valueNext * (nNext - mNext + 1);
                        if (valueNext >= valueLast && nLast >= mNext) {
                            value1Sum = value1Sum - (Math.abs(nLast - mNext) + 1) * valueLast;
                        }
                        if (valueNext < valueLast && nLast >= mNext) {
                            value1Sum = value1Sum - (Math.abs(nLast - mNext) + 1) * valueNext;
                        }
                        valueLast = valueNext;
                        nLast = nNext;
                        count++;
                    }

                }

                if (count == 0) {
                    if (Integer.parseInt(strArr1[0]) == 1) {
                        mLast = Integer.parseInt(strArr1[1]);
                        nLast = Integer.parseInt(strArr1[2]);
                        valueLast = Integer.parseInt(strArr1[3]);
                        value1Sum = valueLast * (nLast - mLast + 1);
                    }
                    count++;
                }
            }

            sum = value1Sum + value2Sum;
            System.out.println(sum);

        }
        in.close();
    }
}

-------------------------------------------

Answers are for reference only

Guess you like

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