SH2018笔试题之成长值问题

一、题目

     详见代码

二、代码

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()) {
            //输入行数row
            int row = in.nextInt();

            //输入成长值
            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]);
            //

            //计算成长值
            int mLast = 0; //每日成长值开始时间
            int nLast = 0; //每日成长值结束时间
            int valueLast = 0;//每日成长值
            int mNext = 0; //每日成长值开始时间
            int nNext = 0; //每日成长值结束时间
            int valueNext = 0;//每日成长值
            int value2Sum = 0; //任务成长值累计值
            int value1Sum = 0; //每日成长值累计值
            int count = 0; //统计每日成长值的数据有多少条
            int sum = 0; //总成长值

            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();
    }
}

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

答案仅供参考

posted @ 2018-04-16 21:08  AI菌  阅读(341)  评论(0编辑  收藏  举报