學會入門技巧,讓你在APCS課程裡奪得先機~(下)

APCS觀念固然重要,但實戰演練也是很重要的!來試試看吧~

 

程式設計實作題

- 題型:共計 4 個題組,以撰寫完整程式或副程式計分。

- 檢測與計分方式:為單節次檢測 (測驗時間 140 分鐘),滿分 400 分

實作題例題

問題描述

一次考試中,於所有及格學生中獲取最低分數者最為幸運,反之,於所有不及格同學中,獲取最高分數者,可以說是最為不幸,而此二種分數,可以視為成績指標。請你設計一支程式,讀入全班成績(人數不固定),請對所有分數進行排序,並分別找出不及格中最高分數,以及及格中最低分數。當找不到最低及格分數,表示對於本次考試而言,這是一個不幸之班級,此時請你印出:「worst case」;反之,當找不到最高不及格分數時,請你印出「best case」。註:假設及格分數為 60,每筆測資皆為 0~100 間整數,且筆數未定。

輸入格式

第一行輸入學生人數,第二行為各學生分數(0~100 間),分數與分數之間以一個空白間格。每一筆測資的學生人數為 1~20 的整數。

輸出格式

每筆測資輸出三行。
第一行由小而大印出所有成績,兩數字之間以一個空白間格,最後一個數字後無空白;
第二行印出最高不及格分數,如果全數及格時,於此行印出 best case;
第三行印出最低及格分數,當全數不及格時,於此行印出 worst case。

範例一:輸入

10
0 11 22 33 55 66 77 99 88 44

範例一:正確輸出

0 11 22 33 44 55 66 77 88 99
55
66

(說明)不及格分數最高為 55,及格分數最低為 66。

範例二:輸入

1
13

範例二:正確輸出

13
13 worst case

(說明)由於找不到最低及格分,因此第三行須印出「worst case」。

範例三:輸入

2
73 65

範例三:正確輸出

65 73
best case
65

(說明)由於找不到不及格分,因此第二行須印出「best case」。

評分說明

輸入包含若干筆測試資料,每一筆測試資料的執行時間限制(time limit)均為 2 秒, 依正確通過測資筆數給分。

針對實作題,我們分別使用 C、PythonJava 來解題,語法如下:

實作題解法#1 - 使用 C 語言(最費時):

#include < stdio.h >
#include < stdbool.h >

int len;
int *scores;

main()
{
    int i, j, tmp, highestUnpass, lowestPass;
    bool best=false, worst=false;
    scanf("%d", &len);
    scores = (int *)malloc(sizeof(int) * len);
    for (i=0;i < len;i++)
        scanf("%d", &scores[i]);
    for (i=0;i < len-1;i++)
        for(j=i+1;j < len;j++)
            if (scores[i] > scores[j])
            {
                tmp = scores[i];
                scores[i] = scores[j];
                scores[j] = tmp;
            }
    for (i=0;i < len;i++)
    {
        printf("%d", scores[i]);
        if (i < len-1)
            printf(" ");        
    }
    printf("\n");
    if (scores[len-1] < 60)
    {
        worst = true;
        highestUnpass = scores[len-1];
    }
    if (scores[0] >= 60)
    {
        best = true;
        lowestPass = scores[0];
    }
    if (worst == false && best == false)
    for (i=0;i < len;i++)
        if (scores[i] >= 60)
        {
            highestUnpass = scores[i-1];
            lowestPass = scores[i];
            break;
        }
    if (best)
        printf("best case\n");
    else
        printf("%d\n",highestUnpass);
    if (worst)
        printf("worst case\n");
    else
        printf("%d\n",lowestPass);
}

實作題解法#2 - 使用 Python 語言(最省時):

nums = int(input())
strScores = input()
scores = strScores.split(" ")
for i in range(len(scores)):
    scores[i] = int(scores[i])
scores.sort()
for i in range(len(scores)):
    print(scores[i], end="")
    if i < len(scores)-1:
        print(" ", end="")
print()
best = False
worst = False
if scores[len(scores)-1] < 60:
    worst = True
    highestUnpass = scores[len(scores)-1]
if scores[0] >= 60:
    best = True
    lowestPass = scores[0]
if best==False and worst == False:
    for i in range(len(scores)):
        if scores[i] > 60:
            lowestPass = scores[i]
            highestUnpass = scores[i-1]
            break
if best:
    print("best case")
else:
    print(highestUnpass)
if worst:
    print("worst case")
else:
    print(lowestPass)

實作題解法#3 - 使用 Java 語言(所花時間適中):

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class T01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        
        String nouse = scanner.nextLine();
        String data = scanner.nextLine();
        String[] strScores = data.split(" ");
        int len = strScores.length;
        int [] intScores = new int[len];
        int i;
        for (i=0;i < len;i++)
        {
            intScores[i] = Integer.parseInt(strScores[i]);
        }
        Arrays.sort(intScores);
        boolean best=false, worst=false;
        int highestUnpass = 0, lowestPass = 0;
        if (intScores[0] >= 60)
        {
            best = true;
            lowestPass = intScores[0];
        }
        if (intScores[intScores.length-1] < 60)
        {
            worst = true;
            highestUnpass = intScores[intScores.length-1];
        }
        
        for (i=0;i < len;i++)
        {
            System.out.print(intScores[i]);
            if (i < len-1)
                System.out.print(" ");              
        }
        System.out.println();
        if (best == false && worst == false)
        {
            for (i=0;i < len;i++)
            {       
                if (intScores[i] > 60)
                {
                    highestUnpass = intScores[i];
                    lowestPass = intScores[i-1];
                    break;
                }           
            }
        }
        if (best)
        {
            System.out.println("best case");
        }
        else
        {
            System.out.println(highestUnpass);
        }
        if (worst)
        {
            System.out.println("worst case");           
        }
        else
        {
            System.out.println(lowestPass);
        }                
    }
}

讓我們來總結一下綜合比較的部分:

- 學習上手速度:Python > Java > C
(但 Python 與其他兩者程式語言差異較大,若一開始就選擇從 Python 語言上手 ,還是必須多花不少時間理解 C 語言,才能解答觀念題。)

- 實際作答速度:Python > Java > C
(Python 與 Java 都具備函式庫,在實作題作答時會比 C 語言快速。)

 

結論:APCS 短期衝刺,從 Java 著手 CP 值最高!

以學習效果與所花時間的比值來說,學習 Java 的 CP 值較高。你問為什麼?讓我來娓娓道來:

雖然 Python 堪稱「程式語言的瑞士刀」,其語法直觀、編寫簡潔快速,比起 C , Java 更容易上手,但由於觀念題是由 C 語言出題,若學 Python 再接觸 C ,對於有時間與其他課業壓力的考生來說,是相當辛苦、費時的。

Java 本身是由 C / C++為概念改良而成的語言,在設計之初,考量重點之一便是簡潔,因此學習與 C 語言語法架構相似的 Java ,讓考生有操作基礎後再學習 C 語言,更能在檢測學習之路,更加如魚得水。

綜合以上觀點,投資在能兼顧「理論題」與「實務題」的 Java,才是事半功倍、投報率最高的首選!

最後貼心提醒:109 年第 2 次 APCS 檢測暫訂 2020 年 7 月 4 日!

 

對了!各位考生可以開始逐步準備 APCS 檢測囉!

 

本篇為下篇,上篇請點此連結

 

 相關閱讀推薦:

學會入門技巧,讓你在APCS課程裡奪得先機~(上)

 

為何APCS認證具備各種優勢?看這裡就知道!(上)

這19個Python語法,讓想入門的你有個明確方向~

 

從哆啦A夢到 iPhone...為何 UI 設計用「圓角」就是比較討喜?

APCS 程式檢定,該從 C、Java 還是 Python 下手?

【前端工程師CSS教學】float浮動屬性

AI人工智慧救地球!Google:AI 保護環境還比人類快 3000 倍

arrow
arrow

    布萊恩的創業小窩 發表在 痞客邦 留言(0) 人氣()