Unit 4 · Vector

Start Here / 開始做題

  1. Read Input and Output first. 中文:先找輸入及輸出。
  2. Write down the variables you need. 中文:先決定變數。
  3. Trace the sample once by hand. 中文:用範例手算一次。
  4. Write, run, then compare every output character. 中文:執行後逐字比較輸出。

Unit Dashboard / 單元地圖

LearnMeaningUse
vector<int> va resizable listunknown amount of data
push_back(x)append one valuebuild a list from input
v.size()current lengthsafe loop boundary
sort(...)ascending orderstatistics and ranking

Quick Concepts / 重點

SyntaxStudent note
vector<int> v;Create an empty integer vector.
v.push_back(x);Add x at the end. 中文:加入最後。
v[i]Valid indexes are 0 to v.size()-1.
for (int x : v)Read every value directly.

From the 2022/23 Notebook / 補充

int maximum = v[0];
for (int x : v) if (x > maximum) maximum = x;
Extra taskInputOutput
Vector Maximum5 / 8 2 11 5 711
Positive / Negative / Zero6 / -2 0 5 -1 3 02 / 2 / 2
Vector Average4 / 61 62 63 6462.50

C++ Syntax / C++ 語法

This is a complete vector program. It reads N values and prints their sum. 中文:可直接執行,讀入 N 個數並輸出總和。

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n, x;
    cin >> n;
    vector<int> v;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
        sum += x;
    }
    cout << sum << endl;
    return 0;
}

Yellow Syntax Guide / 黃色語法重點

SyntaxMeaning
vector<int> v;create an integer vector
v.push_back(x);add x to the end
sum += x;update the total
v.size()number of stored values

Dry Run / 手算追蹤

For values 10 25 76: v becomes [10, 25, 76]; sum becomes 10 → 35 → 111. 中文:每次讀入一個數,就加入 vector 並更新總和。

Examples / 例題

Example 4-1 · Vector Sum

題目說明 / Problem Statement

Read N integers into a vector and print their sum. 中文:讀入 vector 並計算總和。

輸入格式 / Input Format

Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。

輸出格式 / Output Format

Print the sum. 中文:輸出總和。

限制 / Constraints

1 ≤ N ≤ 100; |value| ≤ 10^6

範例輸入 / Sample Input

5
10 25 76 45 78

範例輸出 / Sample Output

234

Hint / 提示:Use push_back() and a loop.

Reference Answer / 參考答案

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n, x;
    cin >> n;
    vector<int> v;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
        sum += x;
    }
    cout << sum << '\n';
    return 0;
}

Example 4-2 · Sort Values

題目說明 / Problem Statement

Read N integers, sort them in ascending order, and print them. 中文:排序 vector。

輸入格式 / Input Format

Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。

輸出格式 / Output Format

Print sorted values separated by spaces. 中文:以空格輸出排序結果。

限制 / Constraints

1 ≤ N ≤ 100

範例輸入 / Sample Input

5
4 2 9 1 6

範例輸出 / Sample Output

1 2 4 6 9

Hint / 提示:Use sort(v.begin(), v.end()).

Reference Answer / 參考答案

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
        if (i > 0) cout << ' ';
        cout << v[i];
    }
    cout << '\n';
    return 0;
}

ClassWork / 課後練習

Complete every task independently. Submit source code and a screenshot of a successful run. 中文:獨立完成,提交程式碼及成功輸出截圖。

ClassWork 7-1 · Reverse Vector

題目說明 / Problem Statement

Read N integers and print them in reverse order. 中文:倒序輸出 vector。

輸入格式 / Input Format

Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。

輸出格式 / Output Format

Print values from last to first. 中文:由最後至最前輸出。

限制 / Constraints

1 ≤ N ≤ 100

範例輸入 / Sample Input

5
1 2 3 4 5

範例輸出 / Sample Output

5 4 3 2 1

Hint / 提示:Use indexes; do not use reverse().

ClassWork 7-2 · Even and Odd Vectors

題目說明 / Problem Statement

Read N integers. Put evens in one vector and odds in another. 中文:分開儲存偶數及奇數。

輸入格式 / Input Format

Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。

輸出格式 / Output Format

Line 1: even values. Line 2: odd values. 中文:第一行偶數;第二行奇數。

限制 / Constraints

1 ≤ N ≤ 100

範例輸入 / Sample Input

5
3 8 1 6 5

範例輸出 / Sample Output

8 6
3 1 5

Hint / 提示:Test each value with % 2.

ClassWork 7-3 · Vector Statistics

題目說明 / Problem Statement

Read integers until -1. Print the maximum, minimum, average to 2 decimals, and sorted values. 中文:輸入至 -1,輸出統計及排序。

輸入格式 / Input Format

One line of integers ending with -1. 中文:一行整數,以 -1 結束。

輸出格式 / Output Format

Print maximum, minimum, average, then sorted values on separate lines. 中文:依序輸出最大、最小、平均、排序。

限制 / Constraints

At least one value before -1; values are 0–10^6.

範例輸入 / Sample Input

4 9 1 6 -1

範例輸出 / Sample Output

9
1
5.00
1 4 6 9

Hint / 提示:Do not store the terminating -1.

ClassWork 8-1 · Frequency Count

題目說明 / Problem Statement

Read N digits (0–9) and print the frequency of each digit from 0 to 9. 中文:統計 0 至 9 每個數字出現次數。

輸入格式 / Input Format

Line 1: N. Line 2: N digits. 中文:第一行 N;第二行 N 個數字。

輸出格式 / Output Format

Print ten counts in one line. 中文:一行輸出十個數量。

限制 / Constraints

1 ≤ N ≤ 1000

範例輸入 / Sample Input

5
1 2 1 0 2

範例輸出 / Sample Output

1 2 2 0 0 0 0 0 0 0

Hint / 提示:Use a vector of size 10.

ClassWork 8-2 · Second Largest

題目說明 / Problem Statement

Given N distinct integers, print the second largest value. 中文:找第二大數值。

輸入格式 / Input Format

Line 1: N. Line 2: N distinct integers. 中文:第一行 N;第二行 N 個相異整數。

輸出格式 / Output Format

Print the second largest value. 中文:輸出第二大值。

限制 / Constraints

2 ≤ N ≤ 100

範例輸入 / Sample Input

5
8 2 11 5 7

範例輸出 / Sample Output

8

Hint / 提示:Update largest and second largest in one scan.

Built with LogoFlowershow