# 第二章、運算以及條件判斷 ## 四加一則運算 | 運算子 | 運算符號 | 範例 | 範例答案 | |:------:|:-------:|:---:|:-------:| | 加法 | + | $1+1$ | 2 | | 減法 | - | $2-1$ | 1 | | 乘法 | * | $2*2$ | 4 | | 除法 | / | $4/2$ | 2 | | 取餘數 | % | $11\%3$ | 2 | :::danger C++遵守運算的優先次序(先乘除餘後加減) 可以用括號改變優先順序 例如 $2+3*4=14$ $(2+3)*4=20$ ::: 有兩種使用方法 1. 可以獨立計算,例如 ```cpp= a=a+3; ``` 2. 也可以在cout或條件判斷時使用,例如 ```cpp= cout<<a+3; ``` :::danger 1.運算過後會依權重轉型態 *權重:double=float>int=long long 2.若是int/int 那麼會做無條件捨去 ::: ## 特殊語法: 在使用+1和-1時,C++ 有特殊語法 可以用 ++ 表示 +1, -\- 表示 -1 ```cpp= int a=10; a++;//a=11 a=a+1;//a=12 a--;//a=11 a=a-1//a=10 ``` :::danger 在條件判斷下,a++會先做條件判斷在+1,++a會先+1再條件判斷,-\-同理 ::: 在a=a+1時也有特殊語法 可以縮減成 ```cpp= int b=10; b=b+1;//b=11 b+=1;//b=12 b=b-1;//b=11 b-=1;//b=10 ``` :::info 例題 [a002. 簡易加法](https://zerojudge.tw/ShowProblem?problemid=a002) ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b<<endl; return 0; } ``` ::: :::warning 習題 [a006. 一元二次方程式](https://zerojudge.tw/ShowProblem?problemid=a006) $$D=\sqrt{b^2-4ac}$$ $$D\begin{cases} \ \geq0,& \text{?? roots} \\[2ex] \ =0,& \text{?? roots} \\[2ex] \ <0,& \text{??root} \end{cases} $$ $$\text{x=}\frac{-b\pm\sqrt{D}}{2 a} $$ ::: :::warning 習題 [d049. 中華民國萬歲!](https://zerojudge.tw/ShowProblem?problemid=d049) ::: :::warning 習題 [d051. 糟糕,我發燒了!](https://zerojudge.tw/ShowProblem?problemid=d051) $°C(x)=(°F(x)-32)*5/9$ ::: :::warning 習題 **APCS 2020.10 I** [f312. 1. 人力分配](https://zerojudge.tw/ShowProblem?problemid=f312) ::: :::warning 習題[TCIRC] [b005: 電電的三角形](https://judge.tcirc.tw/ShowProblem?problemid=b005) ::: ## 條件判斷 ### 各種條件表達 | 中文 | 英文 | C++表示 | | -------- | ---- | ------- | | 等於 | | == | | 不等於 | | != | | 大於 | | > | | 大於等於 | | >= | | 小於 | | < | | 小於等於 | | <= | | 或 | or | \|\| | | 且 | and | && | ### if 語法: :::success ```cpp if(條件式) { 如果成立時執行 } ``` ::: ### if-else 語法: :::success ``` if(條件式) { 如果成立時執行 } else { 條件式未成立時執行 } ``` ::: :::info 例題 [a799. 正值國](https://zerojudge.tw/ShowProblem?problemid=a799) ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n>0) cout<<n<<endl; else cout<<-n<<endl; } ::: :::info 例題 [d068. 該減肥了!](https://zerojudge.tw/ShowProblem?problemid=d068) ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n>50) cout<<n-1<<endl; else cout<<n<<endl; } ``` ::: :::info 例題 [d064. ㄑㄧˊ 數?](https://zerojudge.tw/ShowProblem?problemid=d064) 首先可以知道偶數的定義是$2*n(n為整數)$ 所以可以知道: ```cpp= if(n除2餘0) 是偶數 else 不是偶數 ``` 解答 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a; cin>>a; if(a%2) cout<<"Odd"<<endl; else cout<<"Even"<<endl; } ``` ::: :::warning 習題 [b877. 我是電視迷](https://zerojudge.tw/ShowProblem?problemid=b877) 注意正負! ::: :::warning 習題 [c382. 加減乘除](https://zerojudge.tw/ShowProblem?problemid=c382) ::: :::warning 習題 [d050. 妳那裡現在幾點了?](https://zerojudge.tw/ShowProblem?problemid=d050) 注意正負! ::: :::warning 習題 [d063. 0 與 1](https://zerojudge.tw/ShowProblem?problemid=d063) ::: :::warning 習題 [d060. 還要等多久啊?](https://zerojudge.tw/ShowProblem?problemid=d060) ::: ### if-else if-else 語法: :::success ```cpp if(條件式) { 如果成立時執行 } else if(條件式) { 如果上面都沒有執行且條件式符合時執行 } . . . else { 條件式皆未成立時執行 } ``` ::: 其中else是可以省略的,請依題目來決定 :::info 例題 [d070. 格瑞哥里的煩惱 (0 尾版)](https://zerojudge.tw/ShowProblem?problemid=d065) [k200. 三人行必有我師](https://zerojudge.tw/ShowProblem?problemid=k200) (對於C++來說,這兩題是一模一樣的) 我們可以先有這樣的邏輯架構 ```cpp a=第一個 b=第二個 c=第三個 //&&表示且(and) //||表示或(or) //常用請牢記!! if(a>=b&&a>=c)//A最大 { 輸出a } else if(b>=a&&b>=c) { 輸出b } else//c最大 { 輸出c } ``` 實作 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; if(a>=b&&a>=c) cout<<a<<endl; else if(b>=a&&b>=c) cout<<b<<endl; else cout<<c<<endl; } ``` ::: ::: danger 在未來,我們會有更泛用的寫法,但這個語法也是非常常用,一定要記起來\! ::: :::warning 習題 [d067. 格瑞哥里的煩惱 (1 行版)](https://zerojudge.tw/ShowProblem?problemid=d067) ::: :::warning 習題 [f043. 1. 小豪的回家作業 (Homework)](https://zerojudge.tw/ShowProblem?problemid=f043) TOI 新手同好會 ::: :::warning 習題 [a003. 兩光法師占卜術](https://zerojudge.tw/ShowProblem?problemid=a003) ::: :::warning 習題 [d060. 還要等多久啊?](https://zerojudge.tw/ShowProblem?problemid=a003) 這題融合了所有教過的語法,可以利用這題來檢核自己是否有理解,加油:D :::