語法書 / AA 競程語法書 上冊 / 附錄 / A. 語法速查表

A. 語法速查表

本表涵蓋第一至第七單元的所有核心語法,按單元分組,方便快速查閱。

單元 1:寫出你的第一支程式

語法 用途 範例
#include <iostream> 引入輸入輸出函式庫 #include <iostream>
using namespace std; 使用標準命名空間,免除 std:: 前綴 using namespace std;
int main() { ... } 程式進入點,必須存在 int main() { return 0; }
return 0; 程式結束,回傳 0 表示成功 return 0;
cout << expr << endl; 輸出運算式,自動換行 cout << 42 << endl;
cout << expr << "\n"; 輸出運算式,手動換行(較快) cout << 42 << "\n";
cout << a << b << c; 連鎖輸出(多個 << cout << x << " " << y;
cin >> var; 輸入單一變數 cin >> x;
cin >> a >> b; 連鎖輸入(多個 >> cin >> x >> y;
// 註解 單行註解,編譯器忽略 // 這是註解
/* ... */ 多行註解 /* 行 1<br/>行 2 */
{ ... } 大括號 定義程式碼區塊範圍 if (x > 0) { cout << x; }
int x; 宣告整數變數 int age = 25;

單元 2:資料型態與運算

語法 用途 範例
int 32 位整數(約 ±21 億) int x = 100;
long long 64 位整數(約 ±9 * 10^18) long long big = 1000000000000LL;
char 字元(1 位元組) char c = 'A';
bool 布林值(true 或 false) bool flag = true;
sizeof(type) 查詢型態佔用的位元組數 sizeof(int) // 結果:4
x = y; 指派運算子(右邊給左邊) x = 10;
+=, -=, *=, /=, %= 複合指派運算子 x += 5; // x = x + 5;
++x 前置自增(先加 1,再使用) int a = ++x;
x++ 後置自增(先使用,再加 1) int a = x++;
--x, x-- 前置/後置自減 --count;
+, -, *, /, % 算術運算子 a + b, a - b, a * b, a / b, a % b
1LL, 1000000000LL long long 字面常數(加 LL 後綴) long long x = 1e18;

單元 3:條件判斷

語法 用途 範例
<, >, <=, >= 比較運算子 if (x < 10)
== 相等比較(注意不是 = if (x == 5)
!= 不等比較 if (x != 0)
&& 邏輯「且」(AND) if (x > 0 && x < 10)
|| 邏輯「或」(OR) if (x < 0 || x > 100)
! 邏輯「非」(NOT) if (!flag)
if (cond) { ... } 單向條件 if (x > 0) { cout << "positive"; }
if (cond) { ... } else { ... } 雙向條件 if (x > 0) { ... } else { ... }
if (cond1) { ... } else if (cond2) { ... } else { ... } 多向條件 見範例程式碼
(type)expr 強制型態轉換 (long long)a * b
true, false 布林字面值 bool x = true;

單元 4:迴圈

語法 用途 範例
while (cond) { ... } 當條件為真則重複執行 while (x < 10) { x++; }
for (init; cond; update) { ... } 按計數迴圈 for (int i = 0; i < n; i++)
for (int i = 1; i <= n; i++) 迴圈計數 1 到 n(含) 見範例
for (int i = 0; i < n; i++) 迴圈計數 0 到 n-1(含) 見範例
do { ... } while (cond); 執行至少一次,再檢查條件 do { cin >> x; } while (x != 0);
break; 立刻跳出迴圈 if (x < 0) break;
continue; 跳過本次迭代,進入下一次 if (x % 2 == 0) continue;
i++, ++i, i--, --i 迴圈計數器更新 for (int i = 0; i < n; i++)

單元 5:線上評測系統(概念)

概念 說明
AC (Accepted) 程式輸出正確,測試通過
WA (Wrong Answer) 輸出格式或邏輯錯誤
CE (Compile Error) 程式碼無法編譯
RE (Runtime Error) 程式執行時當機(如陣列越界、除零)
TLE (Time Limit Exceeded) 執行時間超過限制
MLE (Memory Limit Exceeded) 使用記憶體超過限制

單元 6:陣列

語法 用途 範例
int a[n]; 宣告一維陣列(n 是編譯時常數) int a[100];
int a[5] = {1, 2, 3, 4, 5}; 陣列初始化 int a[] = {10, 20, 30};
a[i] 訪問陣列第 i 個元素(0-based) a[0], a[n-1]
const int sz = 1000; 常數陣列大小(不可變) int a[sz];
#define MAXN 1000 宏定義陣列大小 int a[MAXN];
int a[10][10]; 二維陣列宣告 a[i][j]
int a[R][C]; 二維陣列(R 行 C 列) a[0][0] 是左上角

單元 7:函式

語法 用途 範例
return type name(params) { ... } 函式定義 int add(int a, int b) { return a + b; }
void func() { ... } 無回傳值函式 void print() { cout << "Hi"; }
int func(); 函式宣告(前置宣告) main 前宣告
name(args); 函式呼叫 add(3, 5);
return expr; 回傳函式結果 return x + y;
type& param Pass by reference(可修改參數) void inc(int& x) { x++; }
type param Pass by value(無法修改參數) void inc(int x) { x++; }
type param = default_val 預設參數 void print(int x = 0)
static type var; 靜態變數(跨函式呼叫保留值) static int count = 0;
const type& param 常數參考(不可修改) void show(const vector<int>& v)