so call字符串处理?
#include <iostream> #include <map> #include <sstream> #include <string> #include <cstdio> using namespace std; map<string, int> num; string line; string s; void init() { num.insert(make_pair("zero", 0)); num.insert(make_pair("one", 1)); num.insert(make_pair("two", 2)); num.insert(make_pair("three", 3)); num.insert(make_pair("four", 4)); num.insert(make_pair("five", 5)); num.insert(make_pair("six", 6)); num.insert(make_pair("seven", 7)); num.insert(make_pair("eight", 8)); num.insert(make_pair("nine", 9)); num.insert(make_pair("ten", 10)); num.insert(make_pair("eleven", 11)); num.insert(make_pair("twelve", 12)); num.insert(make_pair("thirteen", 13)); num.insert(make_pair("fourteen", 14)); num.insert(make_pair("fifteen", 15)); num.insert(make_pair("sixteen", 16)); num.insert(make_pair("seventeen", 17)); num.insert(make_pair("eighteen", 18)); num.insert(make_pair("nineteen", 19)); num.insert(make_pair("twenty", 20)); num.insert(make_pair("thirty", 30)); num.insert(make_pair("forty", 40)); num.insert(make_pair("fifty", 50)); num.insert(make_pair("sixty", 60)); num.insert(make_pair("seventy", 70)); num.insert(make_pair("eighty", 80)); num.insert(make_pair("ninety", 90)); } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); init(); int t; cin >> t; getline(cin, line); while(t--) { int ans = 0; int a = 0; getline(cin, line); //if(line == "zero") { // cout << 0 << endl; // continue; //} istringstream stream(line); while(stream >> s) { a = num[s]; while(stream >> s) { if(s == "and") continue; if(s == "hundred") { a *= 100; continue; } if(s == "million") { ans = a * 1000000; a = 0; break; } if(s == "thousand") { ans += a * 1000; a = 0; break; } a += num[s]; } } ans += a; /*int tmp1 = ans / 1000000; ans %= 1000000; int tmp2 = ans / 1000; ans %= 1000; if(tmp1 != 0) printf("%d,%03d,%03d/n", tmp1, tmp2, ans); else if(tmp2 != 0) printf("%d,%03d/n", tmp2, ans); else */printf("%d/n", ans); } return 0; }