題目實作

ZSISC32nd 鄭云晶

今彩 539 是一種樂透型遊戲,玩家從 01 ~ 39 的號碼中任選 5 個號碼進行投注。開獎單位將隨機開出 5 個號碼。 

第一列5個整數,代表開出的5個獎號

第二列5個整數,代表購買的彩券號碼  

請輸出 Shiny 總共中了幾個號碼 ?

輸入: 3 13 25 31 39

           20 22 33 25 13

輸出: 2

樂透中獎

*迴圈

*條件判斷

Text

#include <bits/stdc++.h>
using namespace std;

int main() {
    int prize[5], ticket[5];
    for (int i = 0; i < 5; i++) cin >> prize[i];
    for (int i = 0; i < 5; i++) cin >> ticket[i];

    int count = 0;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (prize[i] == ticket[j]) {
                count++;
                break;  // 避免重複計數
            }
        }
    }

    cout << count << endl;
    return 0;
}

*迴圈

*條件判斷

5
3 89
5 -1
10 90
15 0
20 90
83 10
輸入:
輸出:

總分和第一次獲得最高分的時間點

第一行有一個正整數: 提交的次數

總共K有兩個整數:

上傳時間和該次上傳的分數

*第一個提交紀錄不會是嚴重錯誤。
 

*迴圈

*條件判斷

#include<bits/stdc++.h>
using namespace std;

int main() {
    
    int n;   // 總提交次數
    cin >> n;
    
    int max_score = -1;   // 最高分數
    int max_time;   // 第一次拿到最高分數的時間
    int wrong = 0;   // 嚴重錯誤次數
    
    for(int i = 0; i < n; i++) {
        int t, s;
        cin >> t >> s; 
        if(s > max_score) {    // 更新
            max_score = s;
            max_time = t;
        }
        if(s == -1) { // 嚴重錯誤
            wrong++;    
        } 
    }
    
    int score = max(0, max_score - n - wrong * 2);
    cout << score << ' ' << max_time << '\n';
    
    return 0;
}

*若計算出來的分數為負數則計為0

多組測資以 EOF 結束

每組測資兩個正整數 n,m (n,m <= 100000)

接下來一行有 n 個整數依序代表每個食物的飽足度

m 行每行有兩個數字 l,r (1 <= l <= r <= n)

代表你想要吃掉第 l 個到第 r 個食物

3 3
1 2 3
1 3
1 2
2 3
6
3
5
輸入:

輸出:

*陣列

*迴圈

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    int a[100001];

    while (cin >> n >> m) {
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }

        for (int i = 0; i < m; i++) {
            int l, r;
            cin >> l >> r;
            int sum = 0;
            for (int j = l; j <= r; j++) {
                sum += a[j];   // 直接累加
            }
            cout << sum << endl;
        }
    }
    return 0;
}

*布林值

*條件判斷

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c;
    cin>>a>>b>>c;
    bool d=false;   //將布林值d預設為false
    if((a&&b) == c){  // a and b == c 
        d=true;
        cout << "AND" << endl;
	}
    if((a||b) == c){    // a or b == c
        d=true;
        cout << "OR" << endl;
    }
    if(a^b == c){   // a xor b == c
        d=true;
        cout << "XOR" << endl;
    }
    if(d==false) {   //若前三者不符合
        cout << "IMPOSSIBLE" << endl; 
    }
}

}

*字串

*程式從0開始

輸入:263541

輸出:3

#include <iostream>
using namespace std;
  
int main(){
    int even = 0, odd = 0;
    string s;
    cin >> s;
    for (int i=0; i<s.size(); i+=2){
        even += s[i] - '0';
    }
    for (int i=1; i<s.size(); i+=2){
        odd += s[i] - '0';
    }
     
    cout << abs(even - odd) << endl;
  
    return 0;
}

' 3 ' - ' 0 ' = 51 - 48 = 3

*直接使用 '3',實際值是 51(ASCII 碼)不是數字 3

KAHOOT !

Code

By shiny0515