<style> .mkb { background-color: rgba(0, 150, 250, .2); } .mky { background-color: rgba(250, 214, 47, 0.52); } <span class="mky"></span> <span class="mkb"></span> </style> # 算數運算子 | 功能 | 運算子 | 舉例 | | :---- | :-- |:---| | 加 | + |a + b| | 減 | - |a - b| | 乘 | * |a * b| | 除 | / |a / b| | 取餘數(mod) | % |a % b| :::warning 對**整數型態**的變數做除法運算時只會保留整數部分,而非四捨五入。 ::: # 設定運算子(賦值) ## 直接賦值 | 功能 | 運算子 | 舉例| | -------- | -------- |---| | 令a的值為b | = | a = b | ## 運算賦值 | 功能 | 運算子 | 舉例 | 等同於| | --- | --- | --- | ---| | 加法賦值 | += | a += b | a = a + b| | 減法賦值 | -= | a -= b| a = a - b| | 乘法賦值 | *= | a *= b | a = a * b| | 除法賦值 | /= | a /= b| a = a / b | |取餘賦值 | %= | a %= b | a = a % b | :::warning `a += b`的速度比`a = a + b`快一些。 ::: ## 單元運算子 | 功能 | 運算子 | 舉例 | | --- | --- | --- | | 取正 | + | + a | | 取負 | - | - a | | 遞增 | ++ | +\+a, a++ | | 遞減 | -\- | -\-a, a-\- | :::info :::spoiler `++a` 和 `a++` 的區別。 運算子放在變數之前(`++a`)叫前置遞增,反之則為後置遞增。 前置遞增會先`+1`的運算之後再賦值,後置遞增則是先賦值再`+1`。 ```cpp= int n = 1, m = 1; int n2 = n++; //n2 = 1 int m2 = ++m; //m2 = 2 ``` 不過最後 n = m = 2。 ::: ## 關係運算子 | 判斷 | 運算子 | 舉例 | | --- | --- | --- | | 等於 | == | a == b | | 不等於 | != | a != b | | 大於 | > | a > b | | 大於等於 | >= | a >= b | | 小於 | < | a < b | | 小於等於 | <= | a <= b | :::warning 不可寫成 `=>` 或 `=<` 。(是的,助教入班考考題有考這觀念,斬了好多人) ::: ## 邏輯運算子 | 判斷 | 運算子 | 舉例 | | --- | --- | --- | | And(同時符合) | && / and | A && B | | Or(其中一個符合) | \|\| / or | A \|\| B| | Xor(兩者不同) | ^ | A ^ B| :::info :::spoiler 真值表。 |真值表| And | Or | Xor | | --- | --- | --- | --- | | <font color="#f00">False</font>/ <font color="#f00">False</font> | <font color="#f00">False</font> | <font color="#f00">False</font> | <font color="#f00">False</font> | | <font color="#f00">False</font>/ <font color="#0f0">True</font> | <font color="#f00">False</font> | <font color="#0f0">True</font> | <font color="#0f0">True</font> | | <font color="#0f0">True</font>/ <font color="#f00">False</font> | <font color="#f00">False</font> | <font color="#0f0">True</font> | <font color="#0f0">True</font> | | <font color="#0f0">True</font>/ <font color="#0f0">True</font> | <font color="#0f0">True</font> | <font color="#0f0">True</font>| <font color="#f00">False</font> | ::: :::info :::spoiler `!`。 邏輯 "非" 運算: `!` 。 若A = True 則 !A = False,反之若A = False 則 !A = True。 ```cpp= #include <iostream> using namespace std; int main(){ bool a = true, b = false; cout << !a << '\n'; //輸出0 cout << !b << '\n'; //輸出1 return 0; } ``` ::: # 例題 ###### 有什麼題目沒提到或有新增的煩請留言告知。 [a014: 中華民國萬歲!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a014) [a016: 糟糕,我發燒了!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a016) [a029: 分組報告](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a029) [a032: 班際籃球賽](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a032) [a035: 伏林的三角地](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a035) [a042: 買鉛筆](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a042) [a096: U質復旦](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a096) [a329: leolai的完美算術教室](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a329) [a427: 小魔仙講笑話](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a427) [a428: Conversion](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a428) [a438: Weighted Average](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a438) ###### 都看到這了難道不按個愛心支持一下嗎?
×
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