1、題目java
詳見代碼blog
2、代碼it
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(); } }
-------------------------------------------ast
答案僅供參考class