全部评论 2

  • nb,看看我的
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <unordered_map>
    #include <vector>
    using namespace std;
    unordered_map<string, long long> num_map = {
    {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
    {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9},
    {"ten", 10}, {"eleven", 11}, {"twelve", 12}, {"thirteen", 13},
    {"fourteen", 14}, {"fifteen", 15}, {"sixteen", 16}, {"seventeen", 17},
    {"eighteen", 18}, {"nineteen", 19}, {"twenty", 20}, {"thirty", 30},
    {"forty", 40}, {"fifty", 50}, {"sixty", 60}, {"seventy", 70},
    {"eighty", 80}, {"ninety", 90}, {"hundred", 100},
    {"thousand", 1000}, {"million", 1000000}
    };
    long long calc_part(const vector<string>& part) {
    long long res = 0;
    long long temp = 0;
    for (const string& s : part) {
    if (s == "hundred") {
    temp *= 100;
    } else {
    temp += num_map[s];
    }
    }
    res += temp;
    return res;
    }
    int main() {
    string line;
    getline(cin, line);
    stringstream ss(line);
    vector<string> words;
    string word;
    while (ss >> word) {
    words.push_back(word);
    }
    bool is_negative = false;
    int start = 0;
    if (words[0] == "negative") {
    is_negative = true;
    start = 1;
    }
    long long ans = 0;
    vector<string> part;
    for (int i = start; i < words.size(); ++i) {
    if (words[i] == "million") {
    ans += calc_part(part) * 1000000;
    part.clear();
    } else if (words[i] == "thousand") {
    ans += calc_part(part) * 1000;
    part.clear();
    } else {
    part.push_back(words[i]);
    }
    }
    ans += calc_part(part);
    if (is_negative) ans = -ans;
    cout << ans;
    return 0;
    }

    5天前 来自 广东

    0
  • 头文件错了
    应该用#include <bits/stdc++.h>的

    2025-05-07 来自 四川

    0
暂无数据

提交答案之后,这里将显示提交结果~

首页