ZeroJudge E925: 學號檢查

題目敘述

每筆測資第一行有一個正整數N,接下來會有N行,每行會有一個由四個字元所組成的字串。接下來會有10行,每行會有一個由9個字元所組成的字串,要求輸出每一個字串是否可以通過驗證,並且輸出驗證失敗率。

驗證方式:

1. 第一個字元為 'B'

2. 第二和第三個字元為數字

3. 第四到七個字元在收N行資料的時候有出現過

4. 最後兩個字元為數字


範例輸入 #1

76

0000

1000

1010

1011

1020

1030

1040

1050

1060

1070

1090

2000

2010

2020

2030

2040

2070

2080

2090

3000

3021

3022

3023

3030

3050

3100

4000

4010

4020

4030

4031

4040

4060

4080

4081

4090

4120

5000

5010

5020

5040

5050

5070

5080

6000

6010

6020

6030

6050

6060

6070

6080

6090

6100

6110

6120

6130

7000

7011

7012

7020

7030

7040

7050

8000

8010

9000

9010

9020

A000

A011

A012

A013

B000

B010

B020

B00100000

R00100000

BA0100000

B00101300

B001000A0

B09902005

B06A01233

B12701256

B80310020

B98901030

範例輸出 #1

Y

N

N

N

N

Y

Y

Y

Y

Y

0.4


解題思路

可以使用Map來判斷中間四個字元是否有出現過,輸出失敗率時如果沒有驗證失敗要輸出0而不是0.0,如果全部都失敗要輸出1而不是0.10或1.0。

解題程式碼如下 (僅供參考):

#include <iostream>
#include <map>
#include <cctype>
using namespace std;

int main() {
    int N;
    cin >> N;
    map<string, int>MAP;
    for (int i = 0; i<N; i++)
    {
        string str;
        cin >> str;
        MAP[str]++;
    }
    int wrong = 0;
    for (int i = 0; i<10; i++)
    {
        string str;
        cin >> str;
        if (str[0] != 'B')
        {
            wrong++;
            cout << "N\n";
            continue;
        }
        if (!isdigit(str[1]) || !isdigit(str[2]))
        {
            wrong++;
            cout << "N\n";
            continue;
        }
        string tmp = "";
        for (int j = 3; j<=6; j++)
        {
            tmp += str[j];
        }
        if (MAP[tmp] == 0)
        {
            wrong++;
            cout << "N\n";
            continue;
        }
        if (!isdigit(str[7]) || !isdigit(str[8]))
        {
            wrong++;
            cout << "N\n";
            continue;
        }
        cout << "Y\n";
    }
    if (wrong == 0) cout << 0 << "\n";
    else if (wrong == 10) cout << 1 << "\n";
    else cout << "0." << wrong << "\n";
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge M932: 蜜蜂觀察