这道题目使用Map。 然后一次性遍历下来即可。 QAQ
注意初始化的时候小心点不要错..
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #include <list> #include <queue> #include <vector> #include <algorithm> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (((a) < (b)) ? (a) : (b)) #define Abs(x) (((x) > 0) ? (x) : (-(x))) #define MOD 1000000007 #define pi acos(-1.0) using namespace std; typedef long long ll ; typedef unsigned long long ull ; typedef unsigned int uint ; typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;} template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e-7 ; const int N = 210 ; const int M = 1100011*2 ; const ll P = 10000000097ll ; const int MAXN = 100000 ; const int INF = 0x3f3f3f3f ; const int MAX = 10500 ; map <string, int> g; void init(){ g["zero"] = 0, g["one"] = 1, g["two"] = 2, g["three"] = 3, g["four"] = 4; g["five"] = 5, g["six"] = 6, g["seven"] = 7, g["eight"] = 8, g["nine"] = 9; g["ten"] = 10, g["eleven"] = 11, g["twelve"] = 12, g["thirteen"] = 13, g["fourteen"] = 14; g["fifteen"] = 15, g["sixteen"] = 16, g["seventeen"] = 17, g["eighteen"] = 18, g["nineteen"] = 19; g["twenty"] = 20, g["thirty"] = 30, g["forty"] = 40, g["fifty"] = 50, g["sixty"] = 60; g["seventy"] = 70, g["eighty"] = 80, g["ninety"] = 90; } int main(){ std::ios::sync_with_stdio(false); int i, j, t, k, u, v, x, y, numCase = 0; init(); cin >> t; cin.get();// while(t--){ string ss; getline(cin, ss, ' ');// ss += ' '; int len = ss.length(); int num = 0, cur = 0, ans = 0; for(i = 0; i < len; ++i){ if(ss[i] == ' '){ string str = ss.substr(num, i - num); num = i + 1; if(str == "and"){ continue; } else if(str == "million"){ cur *= 1000000; ans += cur; cur = 0; } else if(str == "thousand"){ cur *= 1000; ans += cur; cur = 0; } else if(str == "hundred"){ cur *= 100; } else{ cur += g[str]; } } } ans += cur; cout << ans << endl; } return 0; }