練習題目¶
Problem. 兩數運算¶
任務: 實作:
main() 讀入兩個整數,依序輸出和、差、積。
輸入:
輸出:
限制: 三項運算必須由指定函數完成。
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int main() {
int a, b;
cin >> a >> b;
cout << "和:" << add(a, b) << '\n'
<< "差:" << subtract(a, b) << '\n'
<< "積:" << multiply(a, b) << '\n';
}
Problem. 成績驗證¶
建議檔名:F02_成績驗證.cpp
實作 bool isValidScore(int score),0~100 回傳 true。main() 輸入一個整數,輸出 有效 或 無效。
範例輸入: 105
範例輸出: 無效
#include <iostream>
using namespace std;
bool isValidScore(int score) {
return score >= 0 && score <= 100;
}
int main() {
int score;
cin >> score;
cout << (isValidScore(score) ? "有效\n" : "無效\n");
}
Problem. 溫度轉換¶
實作:
輸入攝氏溫度,輸出華氏溫度,再把結果轉回攝氏。顯示小數點後兩位。
範例輸入: 25
範例輸出:
#include <iomanip>
#include <iostream>
using namespace std;
double toFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
double toCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
int main() {
double celsius;
cin >> celsius;
double fahrenheit = toFahrenheit(celsius);
cout << fixed << setprecision(2)
<< "華氏:" << fahrenheit << '\n'
<< "轉回攝氏:" << toCelsius(fahrenheit) << '\n';
}
Problem. 限制範圍¶
實作 int clamp(int value, int low, int high)。小於 low 回傳 low,大於 high 回傳 high,否則回傳原值。假設 low <= high。
main() 讀入 value low high 並輸出結果。
Problem. 交換與排序兩數¶
實作:
sortTwo 要讓 first <= second;需要交換時必須呼叫 swapValues。
輸入: 9 2
輸出: 2 9
#include <iostream>
using namespace std;
void swapValues(int& first, int& second) {
int temporary = first;
first = second;
second = temporary;
}
void sortTwo(int& first, int& second) {
if (first > second) swapValues(first, second);
}
int main() {
int first, second;
cin >> first >> second;
sortTwo(first, second);
cout << first << ' ' << second << '\n';
}
Problem. 加分函數¶
實作 bool addBonus(int& score, int bonus):
- 原成績或加分為負數時回傳
false,不得修改。 - 否則加分,最高 100,回傳
true。
main() 輸入成績與加分,輸出新成績或錯誤訊息。
#include <iostream>
using namespace std;
bool addBonus(int& score, int bonus) {
if (score < 0 || bonus < 0) return false;
score += bonus;
if (score > 100) score = 100;
return true;
}
int main() {
int score, bonus;
cin >> score >> bonus;
if (addBonus(score, bonus)) cout << score << '\n';
else cout << "輸入不可為負數\n";
}
Problem. 陣列統計¶
實作:
輸入 8 個整數,輸出總和、最大值及最大值第一次出現的索引。空指標或長度 0 時,findMaximum 回傳 nullptr。
#include <cstddef>
#include <iostream>
using namespace std;
int sum(const int* data, size_t size) {
int result = 0;
for (size_t i = 0; i < size; ++i) result += data[i];
return result;
}
const int* findMaximum(const int* data, size_t size) {
if (!data || size == 0) return nullptr;
const int* best = data;
for (const int* current = data + 1; current != data + size; ++current)
if (*current > *best) best = current;
return best;
}
int main() {
int values[8]{};
for (int& value : values) cin >> value;
const int* best = findMaximum(values, 8);
cout << "總和:" << sum(values, 8) << '\n'
<< "最大值:" << *best << '\n'
<< "索引:" << best - values << '\n';
}
Problem. 篩選及格¶
實作 vector<int> filterPassed(const vector<int>& scores)。輸入人數 n 與 n 個成績,輸出所有及格成績;若無人及格,輸出 無。
輸入:
輸出: 60 85 72
#include <iostream>
#include <vector>
using namespace std;
vector<int> filterPassed(const vector<int>& scores) {
vector<int> result;
for (int score : scores)
if (score >= 60) result.push_back(score);
return result;
}
int main() {
int n;
cin >> n;
vector<int> scores(n);
for (int& score : scores) cin >> score;
vector<int> passed = filterPassed(scores);
if (passed.empty()) cout << "無";
else for (int score : passed) cout << score << ' ';
cout << '\n';
}