Input Error!。#include <iostream>
#include <iomanip> // 用於格式對齊
using namespace std;
int main() {
int T;
cin >> T;
// 存儲所有測試用例
int* cases = new int[T * 2];
for (int i = 0; i < T; ++i) {
cin >> cases[i * 2] >> cases[i * 2 + 1];
}
for (int i = 0; i < T; ++i) {
int a = cases[i * 2];
int b = cases[i * 2 + 1];
// 參數校驗
if (a > b || a <= 0 || b >= 10) {
cout << "Input Error!";
} else {
// 輸出第 a 到 b 行
for (int row = a; row <= b; ++row) {
for (int col = 1; col <= row; ++col) {
// 格式:寬度 6 左對齊,確保對齊
cout << row << "*" << col << "=" << row * col;
if (col < row) cout << "\t"; // 或使用 setw
}
cout << endl;
}
}
// 組間空行(最後一組不輸出)
if (i != T - 1) cout << endl;
}
delete[] cases;
return 0;
}
| 技術點 | 實現細節 |
|---|---|
| 動態數組存儲用例 | new int[T*2] 避免固定大小限制,用完 delete[] 釋放 |
| 參數邊界檢查 | a > b || a <= 0 || b >= 10 完全覆蓋題目條件 |
| 乘法表行列邏輯 | 外層循環行 row,內層循環列 col <= row,符合下三角特性 |
| 格式對齊 | 使用 \t 或 <iomanip> setw(6) << left 保證列對齊 |
| 組間空行控制 | if (i != T-1) cout << endl; 最後一組不加空行 |
輸入:
2
2
4
5
0
輸出:
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
\t 或 setw 更可靠。vector> 替代動態數組,更現代安全。setw 寬度。printMultiplicationTable(a, b) 函數,主邏輯更清晰。