# 經過高一資訊課後,你該知道的基礎語法
- [語法](#語法)
- [組合技](#組合技)
## 語法
### cin, cout, int, float, double, bool
```cpp!
int a, b=2;
double money = 1.23;
cin>> money;
cout<< money * 2;
bool decision = true;
```
#### operators
```cpp!
int a=2, b=3;
cout<< a + b; // 5
cout<< a - b; // -1
cout<< a * b; // 6
cout<< a / b; // 0, 整數除法
cout<< a % b; // 2, 取餘數
// a^b, a**b 次方? 沒有這個東西
```
### if
```cpp!
int money = 30;
if(money < 10) {
cout<< "POOR";
}
else if(money < 50) {
cout<< "NORMAL";
}
else {
cout<< "RICH";
}
```
#### operators
| == | != | > | < | >= | <= |
| ---- | ------ | ---- | ---- | -------- | -------- |
| 等於 | 不等於 | 大於 | 小於 | 大於等於 | 小於等於 |
| A && B | A \|\| B | \!B |
| ------ | ------ | ---- |
| A 且 B | A 或 B | 非 B |
### for, while
```cpp!
for(int i=0; i<10; i++) { // 0 ~ 9
cout<< i<< " ";
}
cout<< endl;
for(int i=10; i>=0; i--) { // 10 ~ 0
cout<< i<< " ";
}
cout<< endl;
int i = 0;
while(i < 10) { // 0 ~ 9
cout<< i<< " ";
i++; // i = i + 1;
}
```
### break, continue
```cpp!
for(int i=0; i<1000; i++) {
if (i %2 == 0) {
continue; // 跳過以下內容,進入下次迴圈 (執行 i++)
}
if (i > 100) {
break; // 離開迴圈
}
cout<< i;
}
```
### array
```cpp!
int a[10]={};
for(int i=0; i<10; i++) {
cin>> a[i];
}
for(int i=9; i>=0; i--) {
cout<< a[i]<< " ";
}
```
## 組合技
- [cin + for/while](#cin--forwhile)
輸入 T 組資料,每組先輸入 N,接著有 N 個數字要輸入
無限輸入
- [if + for/while](#if--forwhile)
計算`次數`、`總和`、`最大` `最小`
- [bool + for/while](#bool--forwhile)
偵測某`事件` `有無`發生
- [cin + for/while + array](#cin--forwhile--array)
檢測某些數字是否`完整`
例如: 輸入的 N 個數字中,包含了哪幾個 1~10 的數字,沒包含的又有哪些
### cin + for/while
> 輸入 N,然後輸入 N 個數
```cpp!
int N;
cin>> N;
for(int i=0; i<N; i++) {
int number;
cin>> number; // 重複輸入 N 個數字
}
```
> **有 T 組測試資料**,每次都會輸入 N 個數字,...
```cpp!
int T;
cin>> T;
for(int times=0; times < T; times ++) {
int N;
cin>> N
for(int i=0; i<N; i++) {
int number;
cin>> number
...
}
}
```
> 無限輸入: 有多組測試資料,每次輸入兩個數字,計算總合
```cpp!
int a, b;
while(cin>> a) { // 一直讀一直讀一直讀一直讀一直讀
cin>> b;
cout<< a + b<< endl;
}
```
### if + for/while
> 計算`次數`、`總和`、`最大` `最小`
```cpp!
// !!! 初始化很重要 !!!
int counts = 0;
int sum = 0;
int MAX = -1000000;
int MIN = 1000000;
for(int i=0; i<10; i++) { // 輸入 10 個數字
int n;
cin>> n;
if(n % 2 == 0) {
counts++; // counts = counts + 1;
}
sum += n; // sum = sum + n;
if(n > MAX) {
MAX = n;
}
MIN = min(MIN, n); // min(a, b) 的功能是: 算出 a, b 中的最小值
}
```
### bool + for/while
> 用來偵測`某事件` `有無`發生
> 例如: 偵測輸入的 10 個數字中有沒有偶數,若有,則輸出一次 yeah,無則輸出 nope
```cpp!
// 第一種方案: 沿用 counts
int counts = 0;
for(int i=0; i<10; i++) {
int n; cin>> n;
if(n % 2 == 0) {
counts += 1;
}
}
if(counts == 0) { cout<< "nope"; }
else { cout<< "yeah"; }
// 第二種方案,使用 bool
bool exists = false;
for(int i=0; i<10; i++) {
int n; cin>> n;
if(n % 2 == 0) {
exists = true;
}
}
if(exists == false) { cout<< "nope"; }
else { cout<< "yeah"; }
```
### cin + for/while + array
> 檢測數列是否完整
> 例如: 班級有 34 人,分別是 1~34 號,輸入 33 個不重複的數字,代表準時的學生,找出遲到的那一名學生
```cpp!
int visit[35] = {}; // 使用範圍 1~34, 全部都歸零
for(int i=0; i<33; i++) {
int n; cin>> n;
visit[n] = 1; // 出現了
}
for(int i=1; i<=34; i++) {
if(visit[i] == 0) { // 找到遲到的了!
cout<< i<< endl;
break; // 剩下的座號不用再看了,跳離迴圈
}
}
```
<div style="height: 300px"></div>