---
# System prepended metadata

title: 程式設計

---

# 程式設計


### printf()
- **printf(format string,item1,item2,...);**
    - Formatted characters
        - %c for char
        - %d for int
        - %f for double
        - %s for string
        - %e for 科學記號
        - &m.nf m代表欄寬; n代表精準度
使用方式(印出一般的):
```cpp=
#include<iostream>
using namespace std;
int main() {
    int a=10;
    printf("顯示 %d 數字" , a);
    /*
     * 顯示 10 數字
    */
    
}
```

使用方式(印出%m.nf):
```cpp=
#include<iostream>
#include<iomanip> // for setprecision() , setw()
using namespace std;
int main(){
    printf("%.1f , %.2f\n" , 1.234 , -1.235); // 四捨五入
}
```
![](https://i.imgur.com/izoLQgv.png)

### scanf()
- **scanf(format string,memory address);**
    - Formatted characters
        - %c for char
        - %d for int
        - %f for float , %If for double
        - %s for string

使用方式:
```cpp=
#include<iostream>
using namespace std;
int main() {

    int interger1 , interger2 , sum;
    scanf("%d%d" , &interger1,&interger2); // == cin >> interger1 >> interger2;
    /*
     * & 為指向變數的記憶體位置(參照)
     */
    sum = interger1 + interger2;
    printf("%d\n" , sum);
}
```
### 參照 
```cpp=
int n = 10;      // 定義變數
int *p = &n;     // 定義指標，儲存 n 的位址
int &r = n;      // 定義參照，是 n 的別名
```

上面的程式中，最後一行定義參考，參考一定要初始，例如以下定義無法通過編譯：
```cpp=
int &r; // error: 'r' declared as reference but not initialized
```

下列程式碼把他想成 **我的b儲存的是a的記憶體位置，所以a變了b也會跟著變**
```cpp=
#include<iostream>
using namespace std;
int main() {
    int a=10;
    int &b = a;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    
    a = 20;
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    /*
     * a: 10
       b: 10
       a: 20
       b: 20 */
}
```


### 萬用標題庫

為甚麼要使用這個標題庫? 你可以把他想成一個大總集，可以不必再去include其他標題。

優化執行速度:
```cpp=
ios::sync_with_stdio(0); 
cin.tie(0);
```

```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
     ios::sync_with_stdio(0);
     cin.tie(0);
}
```

實用技巧
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int k;
    while(cin >> k && k) {
        /*
         *  意思為 輸入 k 然後判斷k
         * 假設 k 等於 0 的話就直接結束程式
    }
}
```
#### to_string() 用法
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    string s1 = "Hi you are good " + to_string(123); // 將不是字串的型態轉成字串
    cout << s;
    // Hi you are good 123
}
```
但今天假設沒有用 to_string() 的話
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    string s1 = "Hi you are good " + 123;
    cout << s;
}
```
就會出錯因為字串是不能進行四則運算的
![](https://i.imgur.com/uZ6OoaQ.png)


### Vector

#### 基本用法
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    vector<int> vec = {1,2,3,4,5}; // 定義 向量空間(vec) 裡面的數值為 {1,2,3,4,5}
    vec.push_back(1); // 將數值丟到尾端 所以 vec 會變成 {1,2,3,4,5,6}
    vec.pop_back(); // 將尾端資料刪除 所以 vec 會變成 {1,2,3,4,5}
    int num = vec.szie() // 將變數 num 定義為 vec 的長度 

    for (int i = 0;i < num;i++) { // 將 vec 陣列裡面的資料印出
        cout << vec[i] << " ";
    } // 1 2 3 4 5
}
```

#### sort比較

vector sort 用法
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    vector<int> vec = {4,5,3,4,2,1}; // 定義 向量空間(vec) 裡面的數值為 {1,2,3,4,5}
    sort(vec.begin() , vec.end()); // sort 需放入 頭端跟尾端的資料
    for (int i = 0;i < vec.size();i++) {
        cout << vec[i] << " ";
    } // 1 2 3 4 4 5 
}
```
array sort 用法
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int arr[5] = {4,5,3,1,7};
    int len = sizeof(arr) / sizeof(int); // 計算陣列長度
    sort(arr , arr+len);
    for (int i = 0;i < len;i++) {
        cout << arr[i] << " ";
    } // 1 3 4 5 7
}
```

vector 的好處不必去計算陣列的長度可以直接調用 vec.end() 的尾端資料

#### for auto
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    vector<int> vec = {5,3,4,1,2};
    for (auto it : vec) { // 想成將 vec 裡面的資料一一拉出來 放到 it 裡面
        it += 100; // it 的數值從 105 -> 103 -> 104 -> 101 -> 102
        // vec 內容並不會遭到修改
    }
    // 但這並沒有改變到 vec 裡面的數值
    for (auto& it : vec) { // 加入參照的概念修改 vec 的記憶體位置
        it += 100; // it 的數值從 105 -> 103 -> 104 -> 101 -> 102
        // 但還會順便更改 vec 的內容
        // vec 的數值變成 {105,103,104,101,102}
    }
}
```

### lower_bound() & upper_bound()
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int arr[10] = { 10, 20, 30, 30, 30, 40, 50 };
    int len = sizeof(arr) / sizeof(int);
    int L = lower_bound(arr,arr+len, 30) - arr; // 計算在arr陣列裡比30還小的數字有幾個
    int T = upper_bound(arr,arr+len, 30) - arr; // 計算在arr陣列裡比30還大的數字有幾個
    cout << L << " " << T; // 2 5
}
```



d073
```cpp=
#include<iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    cout << (n+2)/3 << endl;
}
```

a002
```cpp=
#include<iostream>
using namespace std;
int main (){
    int a , b;
    cin >> a >> b;
    cout << a+b;
}
```

d461
```cpp=
#include<iostream>
using namespace std;
int main() {
    int a;
    cin >> a;
    cout << a - 1;
}
```

d060
```cpp=
#include<iostream>
using namespace std;
int main(){
     int a; 
     cin >> a;
     cout << (60-a+25)%60 << endl;
}
```

d827
```cpp=
#include<iostream>
using namespace std;
int main(){
     int n , total;
     cin >> n;
     if (n>12)
          total += 50 * (n/12) + 5 * (n%12);
     else 
          total += n * 5;
     cout << total << endl;
}
```

c776
```cpp=
#include<iostream>
using namespace std;
int main(){
     int n , m;
     cin >> n >> m;
     cout << 6 + 4*(m-1) + (n-1) * (3*m+2)  << endl;
}   
```

d065
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a[50],maxNum=0;
    for (int i = 0;i<3;i++) {
        cin >> a[i];
        if (a[i] > maxNum) 
            maxNum = a[i];
    }
    cout << maxNum;
}
```
d064
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a;
    cin >> a;
    if (a%2 == 0)
        cout << "Even";
    else 
        cout << "Odd";
}
```


d058
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a;
    cin >> a;
    if (a > 0) 
        cout << "1";
    else if (a < 0)
        cout << "-1";
    else
        cout << "0";
}
```

d126
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b;
    while (cin >> a >> b) {
        cout << a*2+b*2 << "\n";
    }
}
```

d066
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b;
    cin >> a >> b;

    if (a >= 8 && a < 17) {
        cout << "At School";
    } else if (a == 7 && b >= 30) {
        cout << "At School";
    } else {
        cout << "Off School";
    }   
}
```

a006
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c,x1,x2,s;
    cin >> a >> b >> c;
    x1=(sqrt(b*b-4*a*c)-b)/(2*a);
    x2=-(sqrt(b*b-4*a*c)+b)/(2*a); 
    s = b*b-4*a*c;
    if (s == 0) cout << "Two same roots x=" << x1;
    else if (s < 0) cout << "No real root";
    else cout << "Two different roots x1=" << x1 << " , x2=" << x2;
}
```

a053
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int N;
    cin >> N;
    if (N > 40) cout << "100";
    else if (N >= 21 && N <= 40) cout << (10*6)+(10*2)+(N-20);
    else if (N >= 11 && N <= 20) cout << (10*6)+((N-10)*2);
    else cout << N*6;
}
```

d460
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a;
    cin >> a;
    if (a >= 60) cout << "399";
    else if (a >= 18 && a < 60) cout << "890";
    else if (a >= 12 && a < 18) cout << "790";
    else if (a >= 6 && a < 12) cout << "590";
    else cout << "0";    
}
```

d273
- 這題很賤假設n=0 , k=0的時候也會算Ok!
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,k;
    while (cin >> n >> k) {
        if (n != 0 && k == 0) {
            cout << "Impossib1e!" << "\n";
            continue;
        } else if (n == 0 && k == 0) {
            cout << "Ok!" << "\n";
            continue;
        } 
        if (n % k == 0) cout << "Ok!" << "\n";
        else if (n % k != 0) cout << "Impossib1e!" << "\n";
    }
}
```

d053
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main () {
    int a,b;
    while (cin >> a >> b) {
        cout << a*b-1 << "\n";
    }
}
```

a861
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main () {
    int H,W,P;
    while(cin >> H >> W) {
        P = 2 * H + 2 * W;
        cout << P << "\n";
    }
}
```

d485
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b;
    cin >> a >> b;
    if (a == b && a%2 != 0) cout << 0;
    else if (a == 1) cout << abs((b/2) - (a/2));
    else cout << abs((b/2) - (a/2)) + 1;
}
```

d039
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int t,a,b,sum;
    cin >> t;
    for (int i = 0;i < t;i++) {
        sum = 0;
        cin >> a >> b;
        sum = (a/3) * (b/3);
        cout << sum << "\n";
    }
}
```

### 10/10

a004
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a;
    while(cin >> a) {
        if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) cout << "閏年\n";
        else cout << "平年\n";
    }
}
```

d069
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int t,year;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> year;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) cout << "a leap year\n";
        else cout << "a normal year\n";
    }
}
```

d070
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main () {
    int year = 1;
    while (cin >> year) {
        if (year == 0) break;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) cout << "a leap year\n";
        else cout << "a normal year\n";
    }
}
```

d071
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main () {
    int year;
    while (cin >> year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) cout << "a leap year\n";
        else cout << "a normal year\n";
    }
}
```

d072
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,year;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> year;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) cout << "Case " << i+1 << ": a leap year\n";
        else cout << "Case " << i+1 << ": a normal year\n";
    }
}
```

### 10/15

a824
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c,sum = 0,t=0;
    cin >> a >> b >> c;
    for (int i = 1;i <= c;i++) {
        if (i % a == 0 || i % b == 0) sum += i;
    }
    sum = sum%26;
    for (char i = 'A' ; i <= 'Z';i++) {
        t += 1;
        if (t == sum) cout << i;
    }
}
```

a024
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,t;
    cin >> a >> b;
    while (a % b) {
        t = a % b;
        a = b;
        b = t;
    }
    cout << b;
}
```

a215
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,m;
    while (cin >> n >> m) {
        int sum = 0;
        for (int i =0;;i++) {
            sum += n + i;
            if (sum > m) {
                cout << i+1 << "\n";
                break;
            }
        }
    }
}
```

d074
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,num,dp[50],maxNum=0;
    cin >> n;
    for (int i = 0;i < n;i++) {
        cin >> dp[i];
    }
    for (int i = 0;i <n;i++) {
        if (dp[i] > maxNum) maxNum = dp[i];
    }
    cout << maxNum;
}
```

c022
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,a,b;
    cin >> n;
    for (int i = 0;i < n;i++) {
        int sum = 0;
        cin >> a >> b;
        for (int j = a; j <= b;j++) 
            if (j%2 == 1) sum += j;
        cout << "Case " << i+1 << ": " << sum << "\n";
    }
}
```

### 10/23

d356
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int k;
    double i = 1,sum = 0;
    cin >> k;
    do {
        sum += 1/i;
        i++;
    }while(k >= sum);
    cout << int(i-1);
}
```

d356
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main () {
    int n,sum = 0;
    while (cin >> n) {
        sum = 0;
        for (int i = 1;n > i;i++) {
            if (n%i == 0)
                sum += i;
        }
        if (sum > n) cout << "盈數" << "\n";
        else if (sum == n) cout << "完全數" << "\n";
        else if (sum < n) cout << "虧數" << "\n";
    }

}
```

d186
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,t = 0;
    while (cin >> a >> b) {
        if (a == 0 && b == 0) break;
        int t = 0;
        for (int i = sqrt(a);i <= sqrt(b);i++) {
            if (pow(i,2) >= a && pow(i,2) <= b) t += 1;
        }
        cout << t << "\n";
    }
}
```

a536
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
  int N,e,t,c,sum = 0,cola = 0,conter = 0;
  cin >> N;
  for (int i = 0; i < N;i++) {
    cin >> e >> t >> c;
    cola = e + t;
    sum = 0;
    while (cola >= c) {
      conter = cola % c;
      sum += cola /= c; 
      cola += conter;
    }
  
    cout << sum << "\n";
  }
}
```

d122
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
  long long int N,sum = 0;
  while (cin >> N) {
    sum = 0;
    while (N != 0) {
      sum += N / 5;
      N /= 5;
    }
     cout << sum << "\n"; 
  }
}
```

### 11/06
c461
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c;
    bool result = false;
    cin >> a >> b >> c;

    // 先將數值變為 0 跟 1
    if (a != 0) a = 1;
    if (b != 0) b = 1;
    // 這裡我用int(a and b) 是因為c為整數 
    // 我不能讓他跟布林值去比
    if (int(a and b) == c) {
        cout << "AND\n";
        result = true;
    }
    if (int(a or b) == c) {
        cout << "OR\n";
        result = true;
    }
    if (int(a xor b) == c) {
        cout << "XOR\n";
        result = true;
    }
    if (result == false) cout << "IMPOSSIBLE\n";
}
```

e283
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {

    ios::sync_with_stdio(0);
    cin.tie(0);

    char letter[16];
    letter[5] = 'A';
    letter[7] = 'B';
    letter[2] = 'C';
    letter[13] = 'D';
    letter[8] = 'E';
    letter[12] = 'F';
    int N;
    while(cin >> N) {
        for (int i = 0;i < N;i++) {
            int x = 0,tmp;
            for (int j = 0; j < 4;j++) {
                cin >> tmp;
                // 當我的數字往前一格就 * 2 (二進位法)
                // 假設是 0 0 0 1 這樣 x = 1
                // 假設是 0 1 0 0 這樣 x = 0*2+0 + 0*2+1 + 1*2+0 + 2*2+0
                x = x * 2 + tmp; 
            }
            cout << letter[x];
        }
        cout << "\n";
    }
}
```

e286
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int tmp1[5];
    int tmp2[5];
    int result = 0;
    for (int i = 0;i < 2;i++) {
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        tmp1[i] = a+b+c+d;

        int x,y,z,s;
        cin >> x >> y >> z >> s;
        tmp2[i] = x+y+z+s;

        if (tmp1[i] > tmp2[i]) result += 1;
        if (tmp1[i] < tmp2[i]) result -= 1;

        cout << tmp1[i] << ":" << tmp2[i] << "\n";
    }
    if (result > 0) cout << "Win";
    else if (result < 0) cout << "Lose";
    else cout << "Tie";
}
```

f312
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    // 答案可能是負的
    int Y1,Y2,n,maxNum = -1000000;
    int a1,b1,c1;
    int a2,b2,c2;
    cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2 >> n;

    for (int i = 0;i <= n;i++) {
        Y1 = a1 * pow(i,2) + b1*i + c1;
        Y2 = a2 * pow((n-i),2) + b2*(n-i) + c2;
        
        if (Y1+Y2 > maxNum) {
            maxNum = Y1+Y2;
        }
    }
    cout << maxNum;
}
```

f605
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,d,sum = 0,count = 0;
    cin >> n >> d;
    for (int i = 0;i < n;i++) {
        int a,b,c;
        cin >> a >> b >> c;
        if (abs(b-a) >= d || abs(c-b) >= d || abs(c-a) >= d) {
            sum += (a+b+c)/3;
            count += 1;
        } 
    }
    cout << count << " " << sum;
}
```

### 11/13
h081
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,D;
    cin >> n >> D;
    int money[n] , sell = 0, buyMoney = 0, sellMoney = 0,profit = 0;
    bool buy = false,first = true;
    for (int i = 0;i < n;i++) {
        cin >> money[i];
    }
    sellMoney = money[0];
    for (int i = 0;i < n;i++) {
        if (first == true) {
            buy = true;
            first = false;
            buyMoney = money[i];
        } else if (buy == false && money[i] <= sellMoney - D) {
            buy = true;
            buyMoney = money[i];
        } else if (buy == true && money[i] >= buyMoney + D) {
            buy = false;
            sellMoney = money[i];
            profit += money[i] - buyMoney;
        }
    }
    cout << profit;
}  
```

i428
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,xi , yi,maxNum = -1000000,minNum = 1000000, bx , by;
    // 先輸入第一個座標位置
    cin >> n >> bx >> by;
    for (int i = 0;i < n-1;i++) {
        // 輸入第二個座標位置
        cin >> xi >> yi;
        // 計算距離
        int distance = abs(xi - bx) + abs(yi - by);
        bx = xi;
        by = yi;
        maxNum = max(maxNum , distance);
        minNum = min(minNum , distance);
    }
    cout << maxNum << " " << minNum;
}
```

h026
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int F,N,times0 = 0,times2 = 0,times5 = 0;
    cin >> F >> N; 
    int y[N];

    for (int i = 0;i < N;i++) {
        cin >> y[i];
    }

    for (int i = 0;i < N;i++) {
        if (times0 == 2) {
            F = 5;
            times0 = 0;
        }
        if (times2 == 2) {
            F = 0;
            times2 = 0;
        }
        if (times5 == 2) {
            F = 2;
            times5 = 0;
        }
        if (F == 0) {
            if (y[i] == 0) {
                times0 += 1;
                cout << y[i] << " ";
                if (i+1 == N) cout << ": Drew at round " << i+1;
                continue;
            }
            else if (y[i] == 2) cout << F << " : Won at round " << i+1;
            else if (y[i] == 5) cout << F << " : Lost at round " << i+1;
            times0 = 0;
            break;
        }
        if (F == 2) {
            if (y[i] == 2) {
                times2 += 1;
                cout << y[i] << " ";
                if (i+1 == N) cout << ": Drew at round " << i+1;
                continue;
            }
            else if (y[i] == 5) cout << F << " : Won at round " << i+1;
            else if (y[i] == 0) cout << F << " : Lost at round " << i+1;
            times2 = 0;
            break;
        }  
        if (F == 5) {
            if (y[i] == 5) {
                times5 += 1;
                cout << y[i] << " ";
                if (i+1 == N) cout << ": Drew at round " << i+1;
                continue;
            }
            else if (y[i] == 0) cout << F << " : Won at round " << i+1;
            else if (y[i] == 2) cout << F << " : Lost at round " << i+1;
            times5 = 0;
            break;
        }
    }
}
```

f579
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,n,ans = 0;
    cin >> a >> b >> n;
    for (int i = 0;i < n;i++) {
        int input,a_count = 0,b_count = 0;
        cin >> input;
        while (input != 0) {
            if (input == a) a_count++;
            else if (input == b) b_count++;
            else if (input == -a) a_count--;
            else if (input == -b) b_count--;
            cin >> input;
        }
        if (a_count > 0 && b_count > 0) ans++;
    }
    cout << ans;
}
```

g275
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 0;i < n;i++) {
        int A = 0,B = 0,C = 0;
        int t1[8],t2[8];
        for (int j = 1;j <= 7;j++) cin >> t1[j];
        for (int j = 1;j <= 7;j++) cin >> t2[j];
        
        // A
        if ((t1[2] != t1[4] && t1[2] == t1[6]) && (t2[2] != t2[4] && t2[2] == t2[6])) {
            A = 1;
        }
        else {
            A = 0;
            cout << "A";
        }

        // B
        if ((t1[7] == 1) && (t2[7] == 0)) {
            B = 1;
        }
        else {
            B = 0;
            cout << "B";
        }

        // C
        if ((t1[2] != t2[2]) && (t1[4] != t2[4]) && (t1[6] != t2[6])) {
            C = 1;
        }
        else {
            C = 0;
            cout << "C";
        }
        if (A&B&C) {
            cout << "None";
        }
        cout << "\n";
    }
}
```

### 11/20

g595
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,total = 0;
    cin >> n;
    int h[n];
    for (int i = 0;i < n;i++) cin >> h[i]; 
    for (int i = 0;i < n;i++) {
        if (i == 0 && h[i] == 0) {
            total += h[i+1];
            continue;
        }
        if (i == n-1 && h[i] == 0 && h[0] > h[i-1]) {
            total += h[0];
            continue;
        } else if (i == n-1 && h[i] == 0 && h[i-1] > h[0]) {
            total += h[i-1];
            continue;
        }
        if (h[i] == 0) {
            if (h[i+1] > h[i-1]) total += h[i-1];
            else total += h[i+1];
        }
    }
    cout << total;
}
```

i399
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c,sum[10] = {0},Max = 0;
    cin >> a >> b >> c;
    sum[a]++;sum[b]++;sum[c]++;
    for (int i = 1;i < 10;i++) {
        if (sum[i] > Max) Max = sum[i];
    }
    cout << Max << " ";
    for (int i = 9;i >= 0;i--) {
        if (sum[i] > 0) cout << i << " ";
    }
}
```

b964
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int student,maxFailed = -1,minPass = 101;
    bool FailedChange = false,PassChange = false;
    cin >> student;
    int grade[student];
    for (int i = 0;i < student;i++) cin >> grade[i];
    int len = sizeof(grade) / sizeof(int);
    sort(grade,grade + len);
    for (int i = 0;i < student;i++) cout << grade[i] << " ";
    for (int i = 0;i < student;i++) {
        if (grade[i] < 60 && grade[i] > maxFailed) {
            FailedChange = true;
            maxFailed = grade[i];
        }
        if (grade[i] >= 60 && grade[i] < minPass) {
            PassChange = true;
            minPass = grade[i];
        }
    }
    cout << "\n";
    if (maxFailed == -1 && FailedChange == false) cout << "best case\n";
    else cout << maxFailed << "\n";
    if (minPass == 101 && PassChange == false) cout << "worst case";
    else cout << minPass;
}
```

c290
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int A = 0,B = 0;
    string num;
    cin >> num;
    for (int i = 0;i < num.size();i++) {
        if (i % 2 == 0) A += num[i] - '0';
        else B += num[i] - '0';
    }
    cout << abs(A - B);
}
```

c294
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a[3];
    for (int i = 0;i < 3;i++) cin >> a[i];
    sort(a,a+3);
    for (int i = 0;i < 3;i++) cout << a[i] << " ";
    cout << "\n";
    if (a[0]+a[1] < a[2]) cout << "No\n";
    else if (a[0]*a[0] + a[1]*a[1] == a[2]*a[2])  cout << "Right\n";
    else if (a[0]*a[0] + a[1]*a[1] > a[2]*a[2])  cout << "Acute\n";
    else if (a[0]*a[0] + a[1]*a[1] < a[2]*a[2]) cout << "Obtuse\n";
}
```

### 11/27

c295
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int N , M , S = 0,times = 0;
    cin >> N >> M;
    int num[N][M] = {0};
    int tmp[N] = {0};
    for (int i = 0;i < N;i++) {
        int maxNum = -1;
        for (int j = 0;j < M;j++) {
            cin >> num[i][j];
            if (num[i][j] > maxNum) {
                maxNum = num[i][j];
            }
        }
        tmp[i] = maxNum;
        S += maxNum;
    }
    cout << S << "\n";
    for (int i = 0;i < N;i++) {
        if (S % tmp[i] == 0) {
            if (times == 0) cout << tmp[i];
            else cout << " " << tmp[i];
            times += 1;
        }
    }
    if (times == 0) cout << "-1\n";
}
```

c291
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int N;
    cin >> N;
    int group[N] , fr[N];
    for (int i = 0;i < N;i++) {
        group[i] = -1;
        cin >> fr[i];
    }
    for (int i = 0;i < N;i++) {
        if (group[i] >= 0) continue;
        int now = i;
        group[i] = i;
        while (fr[now] != i) {
            group[fr[now]] = i;
            now = fr[now];
        }
    }
    int ans = 0;
    for (int i = 0;i < N;i++) {
        ans += (group[i] == i);
    }
    cout << ans << "\n";
}
```


d563
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n , ans = 0 ,prefix = 0,suffix = 0;
    cin >> n;
    int x[n];
    for (int i = 0;i < n;i++) cin >> x[i];
    prefix = x[0];
    suffix = x[n-1];
    for (int i = 0,j = n-1;i < n && j >= 0;) {
        if (prefix == suffix) {
            ans += 1;
            prefix += x[++i];
        } else if (prefix > suffix) {
            suffix += x[--j];
        } else if (suffix > prefix) {
            prefix += x[++i];
        }
    }
    cout << ans;
}
```

a693
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,m,f,t;
    int satiety[100010];
    while (cin >> n >> m) {
        for (int i = 1;i <= n;i++) {
            cin >> satiety[i];
            satiety[i] += satiety[i - 1];
        }
        for (int i = 1;i <= m;i++) {
            cin >> f >> t;
            cout << satiety[t] - satiety[f-1] << "\n";
        }
    }
}
```

### 12/4
d478
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n , m;
    cin >> n >> m;
    int num1[m],num2[m];
    while (n--) {
        for (int i = 0;i < m;i++) {
            cin >> num1[i]; 
        }
        for (int i = 0;i < m;i++) {
            cin >> num2[i];
        }
        int i = 0, j = 0 , ans = 0;
        while (i < m && j < m) {
            if (num1[i] == num2[j]) {
                ans++;
                i++;
            } else if (num1[i] < num2[j]) {
                i++;
            } else {
                j++;
            }
        }
        cout << ans << "\n";
    }
    
}
```

d596
```cpp=
#include<bits/stdc++.h>
using namespace std;
vector <int> v[10] = {{} , {2,4}, {1,3,5},{2,6},{1,5,7},{2,4,6,8},{3,5,9},{4,8},{5,7,9},{6,8}};
/*
    1 2 3
    4 5 6
    7 8 9
*/
int main(int argc, const char * argv[]) {
    int t, ans[10] = {0};
    cin >> t;
    while (t--) {
        int a,b,c;
        cin >> a >> b >> c;
        for (int i:v[a]) { // 當我的 i 在 v[a] 這個裡面的數字時，就 ans[i] = 1;
            ans[i] = 1;
        }
        for (int i:v[b]) {
            ans[i] = 0;
        }
        for (int i:v[c]) {
            ans[i] = 0;
        }
        bool flag = false;
        for (int i = 1;i < 10;i++) {
            if (ans[i]) {
                ans[i] = 0;
                cout << i << " ";
                flag = true;
            }
        }
        if (!flag) cout << "Empty";
        cout << "\n"; 
    }
} 
```

d481
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c,d;
    while (cin >> a >> b >> c >> d) {
        if (b != c) {
            cout << "Error\n";
            continue;
        }
        long long mat1[a][b],mat2[c][d];
        for (int i = 0;i < a;i++) {
            for (int j = 0;j < b;j++) {
                cin >> mat1[i][j];
            }
        }
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < d;j++) {
                cin >> mat2[i][j];
            }
        }
        long long ans[a][d];
        for (int i = 0;i < a;i++) {
            for (int j = 0;j < d;j++) {
                ans[i][j] = 0;
                for (int k = 0;k < c;k++) {
                    ans[i][j] += mat1[i][k] * mat2[k][j];
                }
            }
        }
        for (int i = 0;i < a;i++) {
            for (int j = 0;j < d;j++) {
                cout << ans[i][j] << " ";
            }
            cout << "\n";
        }
    }
}
```

a694
```cpp=
#include<bits/stdc++.h>
using namespace std;
int food[510][510];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m,x1,x2,y1,y2;
	while (cin >> n >> m) {
		for (int i = 1;i <= n;i++) {
			for (int j = 1;j <= n;j++) {
				cin >> food[i][j];
				food[i][j] +=  food[i-1][j] + food[i][j-1] - food[i-1][j-1];
			}
		}
		for (int i = 0;i < m;i++) {
			cin >> x1 >> y1 >> x2 >> y2;	
			cout << food[x2][y2] - food[x2][y1 - 1] - food[x1 - 1][y2] + food[x1 - 1][y1 - 1] << "\n";	
		}
	}
} 
```

### 2/20

b965
```cpp=
#include<bits/stdc++.h>
using namespace std;
int R,C,M,a[10][10],b[10][10];

void rRotate() {
    for (int i = 0;i < R;i++) {
        for (int j = 0;j < C;j++) {
            b[i][j] = a[i][j];
        }
    }
    for (int i = 0;i < R;i++) {
        for (int j = 0;j < C;j++) {
            a[C-1-j][i] = b[i][j];
        }
    }
    swap(R,C);
}

void rFlip() {
    for (int i = 0;i < R;i++) {
        for (int j = 0;j < C;j++) {
            b[i][j] = a[i][j];
        }
    }

    for (int i = 0;i < R;i++) {
        for (int j = 0;j < C;j++) {
            a[R-i-1][j] = b[i][j];
        }
    }
}

int main() {
    while (cin >> R >> C >> M) {
        int k[M];
        for (int i = 0;i < R;i++) {
            for (int j = 0;j < C;j++) {
                cin >> a[i][j];
            }
        }
        for (int i = 0;i < M;i++) cin >> k[i];

        for (int i = M - 1; i >= 0; i--) { // 要逆著運行
            if (k[i] == 0) rRotate();
            else rFlip();
        }
        cout << R << " " << C << "\n";
        for (int i = 0;i < R;i++) {
            for (int j = 0;j < C;j++) {
                if (j == C - 1) cout << a[i][j] << "\n";
                else cout << a[i][j] << " "; 
            }
            
        }
    
    }

}
```