iT邦幫忙

0

怎麼把這段改成C++

  • 分享至 

  • xImage
while True:
    try:
        dict1 = {'A': 10, 'J': 18, 'S': 26, 'B': 11, 'K': 19, 'T': 27, 'C': 12, 'L': 20, 'U': 28,
                 'D': 13, 'M': 21, 'V': 29, 'E': 14, 'N': 22, 'W': 32, 'F': 15, 'O': 35, 'X': 30,
                 'Y': 31, 'G': 16, 'H': 17, 'Q': 24, 'Z': 33, 'I': 34, 'R': 25, 'P': 23}
        id_text = input()
        en = str(dict1[id_text[0].upper()])
        total = int(en[0]) + int(en[1]) * 9 + int(id_text[len(id_text) - 1])
        id_text = id_text[1:9]
        n = 8
        for i in id_text:
            total += int(i) * n
            n -= 1

        print("real" if total % 10 == 0 else "fake")

    except:
        break
dragonH iT邦超人 5 級 ‧ 2020-06-14 23:35:51 檢舉
it幫compiler
krarm iT邦好手 1 級 ‧ 2020-06-15 17:14:51 檢舉
你先寫一份出來瞧瞧
C++的關聯陣列要用到STL
不過這題也有偷吃步
提示: 'A' is a char!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2022-12-31 00:31:54

怎麼把這段改成C++

#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::unordered_map<char, int> dict1 = {
        {'A', 10}, {'J', 18}, {'S', 26}, {'B', 11}, {'K', 19}, {'T', 27}, 
        {'C', 12}, {'L', 20}, {'U', 28}, {'D', 13}, {'M', 21}, {'V', 29}, 
        {'E', 14}, {'N', 22}, {'W', 32}, {'F', 15}, {'O', 35}, {'X', 30}, 
        {'Y', 31}, {'G', 16}, {'H', 17}, {'Q', 24}, {'Z', 33}, {'I', 34}, 
        {'R', 25}, {'P', 23}
    };

    std::string id_text;
    while (std::cin >> id_text) {
        int total = 0;
        char first_char = toupper(id_text[0]);
        int en = dict1[first_char];
        total += en / 10 + en % 10 * 9 + id_text[id_text.size() - 1] - '0';
        id_text = id_text.substr(1, 8);
        int n = 8;
        for (char c : id_text) {
            total += (c - '0') * n;
            n--;
        }

        std::cout << (total % 10 == 0 ? "real" : "fake") << std::endl;
    }

    return 0;
}

我要發表回答

立即登入回答