【題解】ZeroJudge e925: pD. 學號檢查

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e925

#include <iostream>
#include <set>
using namespace std;

bool notNum (char c){
    if ('0' <= c && c <= '9') return false;
    else return true;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N;
    string s;
    set<string> st;
    cin >> N;
    while (N--){
        cin >> s;
        st.insert(s);
    }
    int correct = 0, wrong = 0;
    while (cin >> s){
        if (s[0] != 'B'){
            wrong++;
            cout << "N\n";
        } else if (notNum(s[1]) || notNum(s[2])){
            wrong++;
            cout << "N\n";
        } else if (st.count(s.substr(3, 4)) == 0){
            wrong++;
            cout << "N\n";
        } else if (notNum(s[7]) || notNum(s[8])){
            wrong++;
            cout << "N\n";
        } else {
            correct++;
            cout << "Y\n";
        }
    }
    cout << (double)wrong / (correct + wrong) << endl;
    return 0;
}

Python code (credit: Amy Chou)

def notNum(c):
    c = ord(c)
    if c >= 48 and c <= 57:
        return False
    else:
        return True
    
N = int(input())
st = set()
for _ in range(N):
    st.add(input())
    
correct = 0
for _ in range(10):
    s = input()
    if s[0] != 'B':
        print("N")
    elif notNum(s[1]) or notNum(s[2]):
        print("N")
    elif s[3:7] not in st:
        print("N")
    elif notNum(s[7]) or notNum(s[8]):
        print("N")
    else:
        correct += 1
        print("Y")
if correct == 10:
    print(0) # avoid 0.0
else:
    print(f"{(10-correct)/10}")
分享本文 Share with friends