java&python-APCS-2023/1/8實作第一題:程式考試 === ### ZeroJudge:j605程式考試 ### <font color="#FFCC22">(for新手)簡單題分析</font> --- \ \ \ \ \ \ <font color="#FFEE99">**1輸入方式**</font> --- n是資料數量 先初始化timeList和scoreList串列,用迴圈把每一項資料用append加入\ 如此一來,當索引是k的時候,對到的scoreList[k]資料就是在timeList[k]的時候送出的,不用怕會亂掉 ```python= n = int(input()) timeList = [] scoreList = [] for _ in range(n): time, score = map(int, input().split()) timeList.append(time) scoreList.append(score) ``` ```java= import java.util.*; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList timeList = new ArrayList(); ArrayList scoreList = new ArrayList(); int time, score; for (int i = 0; i < n; i++) { time = sc.nextInt(); score = sc.nextInt(); timeList.add(time); scoreList.add(score); } sc.close(); ``` \ \ \ <font color="#FFEE99">**2繳交次數**</font> --- 用len()統計總共有幾筆資料\ 或者可以直接使用n ```python= turnInTimes = len(timeList) ``` ```python= turnInTimes = n ``` ```java= int turnInTimes = timeList.size(); ``` ```java= int turnInTimes = n; ``` \ \ \ <font color="#FFEE99">**3最大分數**</font> --- 使用max()內建函數直接找出scoreList最大的分數 ```python= maxScore = max(scoreList) ``` ```java= int maxScore = (int)Collections.max(scoreList); ``` \ \ \ <font color="#FFEE99">**4最大分數送出的時間**</font> --- .index()用來找出一個元素在List中的索引值\ scoreList.index(maxScore)就是送出的索引,跟送出的時間是相同的索引值 ```python= maxScoreTurnInTime = timeList[scoreList.index(maxScore)] ``` ```java= int maxScoreTurnInTime = (int)timeList.get(scoreList.indexOf(maxScore)); ``` \ \ \ <font color="#FFEE99">**5重大錯誤的次數**</font> --- .count()用來找一個元素在List出現的次數 ```python= failTimes = scoreList.count(-1) ``` ```java= int failTimes = Collections.frequency(scoreList, -1); ``` \ \ \ <font color="#FFEE99">**6計算最後成績**</font> --- 原始最大成績-送出次數-重大錯誤次數\*2 千萬別忘了,低於0分要以0分計算 ```python= finalScore = maxScore-turnInTimes-failTimes*2 if finalScore < 0: finalScore = 0 ``` ```java= int finalScore = maxScore-turnInTimes-failTimes*2; if (finalScore < 0) { finalScore = 0; } ``` :::success **6.5**(for想要多學的人)如果不想要用if就讓低於0分的分數直接等於零的方式(看不懂可略): ```python= finalScore = (abs(finalScore)+finalScore) // 2 ``` ```java= finalScore = (Math.abs(finalScore) + finalScore) / 2; ``` ::: \ \ \ <font color="#FFEE99">**7輸出:**</font> --- ```python= print(finalScore, maxScoreTurnInTime) ``` ```java= System.out.println(finalScore+" "+maxScoreTurnInTime); ``` \ \ \ <font color="#FFEE99">**完整程式碼**</font> --- ```python= n = int(input()) timeList = [] scoreList = [] for _ in range(n): time, score = map(int, input().split()) timeList.append(time) scoreList.append(score) turnInTimes = len(timeList) maxScore = max(scoreList) maxScoreTurnInTime = timeList[scoreList.index(maxScore)] failTimes = scoreList.count(-1) finalScore = maxScore-turnInTimes-failTimes*2 if finalScore < 0: finalScore = 0 # finalScore = (abs(finalScore) + finalScore) // 2 print(finalScore, maxScoreTurnInTime) ``` <font color="#FFEE99">**JAVA完整程式碼**</font> --- ```java= package test1; import java.util.*; public class apcs2023_01_1 { public static void main(String[] argv) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList timeList = new ArrayList(); ArrayList scoreList = new ArrayList(); int time, score; for (int i = 0; i < n; i++) { time = sc.nextInt(); score = sc.nextInt(); timeList.add(time); scoreList.add(score); } sc.close(); int turnInTimes = timeList.size(); int maxScore = (int)Collections.max(scoreList); int maxScoreTurnInTime = (int)timeList.get(scoreList.indexOf(maxScore)); int failTimes = Collections.frequency(scoreList, -1); int finalScore = maxScore-turnInTimes-failTimes*2; if (finalScore < 0) { finalScore = 0; } // finalScore = (Math.abs(finalScore) + finalScore) / 2; System.out.println(finalScore+" "+maxScoreTurnInTime); } } ``` ###### tags: `題解` ###### tags: `APCS` ###### tags: `python` ###### tags: `java`