Test 1 · Loop and Condition
Test 1 · Loop and Condition
第一次測驗:迴圈與條件判斷
#A1
# 1. Summation and Loop
# Problem Description:
# Write a C++ program to input an integer n (1 ≤ n ≤ 1000) and calculate the sum from 1 to n.
# Input Format: A single integer n.
# Output Format: Display both the addition process and the total sum, e.g. 1+2+3+...+n=sum.
# Core Concepts: for loop, arithmetic addition, if condition for output formatting.
# 中文說明: 輸入一個整數 n,顯示 1 到 n 的加總過程與結果,例如 1+2+3+...+n=總和。
#include <iostream>
using namespace std;
int main()
{
int ans=0;
int n;
cin>>n; //input
for(int i=1; i<=n-1; i++){
cout<<i<<"+";
ans=ans+i;
}
ans=ans+n;
cout<<n<<"="<<ans;
return 0;
}
# A2
# 2. Divisibility and Counting
# Problem Description:
# Write a C++ program to find all numbers between 1 and 500 that are divisible by both 5 and 11. Count them and show the total
# count.
# Input Format: None.
# Output Format: Print all numbers separated by spaces, then display the total count.
# 找出 1 到 500 之間能同時被 5 與 11 整除的數字,列印並統計總數。
#include <iostream>
using namespace std;
int main()
{
int count=0;
for(int i=1; i<=500; i++){
if(i%5==0 && i%11==0){
cout<<i<<" ";
count=count+1;
}
}
cout<<count;
return 0;
}
# A3
# 3. BMI Calculator
# Problem Description:
# Write a C++ program that inputs a person’s weight (kg) and height (m), then calculates and displays the BMI value with two
# decimal places.
# Input Format: Two double values (weight and height).
# Output Format: Example: Your BMI is: 19.59
# 輸入體重與身高,輸出兩位小數的 BMI 值。
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
double w;
double h;
double bmi;
cin>>w;
cin>>h;
bmi=w/(h*h);
cout<< fixed << setprecision(2) << bmi;
return 0;
}
# A4
# 4. Conditional Pattern and Logic
# Problem Description:
# Write a program to calculate the following series using loop and if statements:
# 2^2 - 3^2 + 4^2 - 5^2 + 6^2 - 7^2
# Input Format: None.
# Output Format: Display the final result of the calculation.
# 使用迴圈與 if 條件計算交錯正負的平方總和:2²−3²+4²−5²+6²−7²。
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
int ans=0;
for(int i=2; i<=7; i++){
if(i%2==0){
ans=ans+(i*i);
}else{
ans=ans-(i*i);
}
}
cout<<ans;
return 0;
}
#B1
# 1. Product of Odd Numbers
# Problem Description:
# Write a C++ program that inputs an integer n (1 ≤ n ≤ 20) and calculates the product of all odd numbers from 1 to n.
# Input Format: A single integer n.
# Output Format: Display the multiplication process and the final product.
# Example: Input 7 → Output 1×3×5×7=105
# 中文說明: 輸入一整數 n,計算並顯示 1 到 n 所有奇數的連乘結果與過程。
#B2
# 2. Find Numbers Divisible by 9
# Problem Description:
# Write a C++ program to print all numbers between 100 and 500 that are divisible by 9. Show the sum and the count of such
# numbers.
# Input Format: None.
# Output Format: Display all qualified numbers and then output the sum and count.
# 中文說明: 找出 100 到 500 之間能被 9 整除的所有數字,顯示總數及總和。
#B3
# 3. Average Score Calculator
# Write a C++ program that inputs 3 double values and prints their average with two decimal places.
# 輸入 3 個分數(double),計算其平均值,並輸出小數點後 兩位。
# Input:
# Three double values separated by spaces.
# 輸入三個分數,以空白分隔。
# Output format:
# Average score = xx.xx (一定要保留小數點後兩位)
# Example 1
# Input → 90 82.5 85
# Output → Average score = 85.83
# Example 2
# Input → 72.5 88 91.25
# Output → Average score = 83.92
# 提示
# ??? a, b, c;
# cin >> a >> b >> c; // 用空格分隔輸入三個分數
#B4
# 4. Alternating Sum of Squares
# Problem Description:
# Use loop and condition statements to compute the following expression:
# 1^2 - 2^2 + 3^2 - 4^2 + 5^2
# Input Format: None.
# Output Format: Display the result of the calculation.
# 使用迴圈與條件式,計算 1²−2²+3²−4²+5² 的結果。