# C++ 基本語法 ## 一、標頭檔 ```cpp= #include <iostream>//基本c++函式庫,包含cin...... #include <vector> ...... ``` #### 基本上就是導入各種函式庫,你才能夠使用他附帶的函式 #### ~~萬用標頭檔~~ ```cpp= #include <bits/stdc++.h> ``` #### 導入了90%會用到的函式庫,但某些judge不給用 #### 注意:大部分函式都需要加上std::,但為了方便,通常要加上 ```cpp= using namespace std; ``` #### 有時候為了不要打那麼多字或者方便閱讀程式碼,也可以使用#define ```cpp= #define loli long long loli a=100; ``` #### 這樣程式在編譯的時候就會自動把loli當成long long(好色 #### 除了標頭檔跟#define以外,打完一行都要加上分號,但通常有大括號的不用加分號 ## 二、基本變數、陣列 #### 常用變數類型 #### 宣告方法 ```cpp= int a;//整數 -(2^31) ~ (2^31)-1 約2e10 long long b;//更長的整數 -(2^63) to (2^63)-1 約9e18 float c;//小數 double d;//更精確的小數 long double f;//更長的更精確的小數 char e;//字元拉黑 string s;//字串拉黑 ``` #### 提取方法(宣告完直接用) Eg. ```cpp= int a=0;//宣告整數a為0; a=a+1;//把a改成a+1 a++; //把a+1 a+=2 //把a+2 ``` #### 陣列(就是一堆變數) #### 宣告方法 ```cpp= int a[n];//n項int陣列(double,float...數字的都可以用這樣宣告) char b[n];//n項char陣列,很像string的咚咚 ``` #### 提取方法(注意是從第0項開始編號) ```cpp= int a[10];//宣告有10項的a陣列 a[0]=1;//把a陣列的第一項改成1; ``` #### 補充:string 本質上也是陣列(字元的)所以也可以用[]提取 ## 三、輸入、輸出 #### 輸入:cin(要加>>),scanf(scanf比較快,但cin比較方便,建議用cin) ```cpp= int a; cin >> a;//輸入a的值 string s; std::cin >> s;//輸入字串串 ``` #### 輸出:基本上與cin一樣,不過>>改成<< ```cpp= int a; cin >> a; a = a + 1; cout << a << endl;//endl為換行 ``` ## 四、for跟while迴圈 #### for迴圈格式 #### for(進入迴圈前的操作;迴圈繼續進行的條件;做完一次迴圈後的操作){要進行的東西} ```cpp= for(int i=0;i<=10;i=i+1) //先定義i=0;i<=10的時候進行,每次進行完i+1; { //放你要做的事 } ``` #### while迴圈格式 #### while(迴圈繼續的條件){要進行的東西} ```cpp= int i=0; while(i<=10)//i<=10的時候進行 { //你要進行的操作 } ``` #### 注意:條件式如果一直進行停不下來的話程式會爛掉 ## 五、條件式 #### if格式 #### if(條件式){要進行的東西} ```cpp= int i=0; if(i==0) { //要進行的東東 } ``` #### else if 格式 #### else if(條件式){要進行的東西} ```cpp= int i=0; if(i==1){ ... } else if(i==0) { .... } ``` #### else 格式 #### else{要進行的東西} ```cpp= int a,b; if(a==b) { .... } else { .... } ``` ## 六、基本模板 ```cpp= #include <iostream> using namespace std; int main()//main函式是主要函式,運行程式就是從這裡開始運行的 { } ``` ## 七、函式 #### 寫在主函式外,要用的時候直接拿 Eg. ```cpp= int sum(int a,int b)//前方int為函式類型,int需要回傳整數,void則不用回傳 //sum為函式名稱,括號內為傳入函式的參數 { return a+b;//回傳a+b } int main() { int a,b; cin >> a >> b; int c=sum(a,b);//因為回傳值為整數,所以可以直接視為數字 cout << c << endl; } ``` ## 八、四(五)則運算 ```cpp= int a,b; a=a+b;//加法 a=a-b;//減法 a=a*b;//乘法 a=a/b;//除法 a=a%b;//取餘數 ``` ## 線上解題網站 [**Zerojudge**](https://zerojudge.tw/) [**TCIRC**](https://judge.tcirc.tw/) [**CodeForces**](https://codeforces.com/) [**Atcoder**](https://atcoder.jp/home) [**CSES**](https://cses.fi/problemset/list/) ## 類題 [**Weird Algorithm**](https://cses.fi/problemset/task/1068) [**Missing Number**](https://cses.fi/problemset/task/1083) [**Repetitions**](https://cses.fi/problemset/task/1069) [**ZJ-a002: 簡易加法**](https://zerojudge.tw/ShowProblem?problemid=a002) [**ZJ-a003: 兩光法師占卜術**](https://zerojudge.tw/ShowProblem?problemid=a003) [**TCIRC-a014: 中華民國萬歲!**](https://judge.tcirc.tw/ShowProblem?problemid=a014) [**TCIRC-a019: 0 與 1**](https://judge.tcirc.tw/ShowProblem?problemid=a019) [**TCIRC-a020: ㄑㄧˊ 數?**](https://judge.tcirc.tw/ShowProblem?problemid=a020) [**A .Elephant**](https://codeforces.com/contest/617/problem/A) [**A. Watermelon**](https://codeforces.com/problemset/problem/4/A) [**A. Next Round**](https://codeforces.com/problemset/problem/158/A) [**A. Bear and Big Brother**](https://codeforces.com/problemset/problem/158/A) [**B. File Name**](https://codeforces.com/contest/978/problem/B) [**A - Move Right**](https://atcoder.jp/contests/abc247/tasks/abc247_a) [**A - Last Letter**](https://atcoder.jp/contests/abc244/tasks/abc244_a)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up