这道题目使用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- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ;
const int MAX = ; map <string, int> g; void init(){
g["zero"] = , g["one"] = , g["two"] = , g["three"] = , g["four"] = ;
g["five"] = , g["six"] = , g["seven"] = , g["eight"] = , g["nine"] = ;
g["ten"] = , g["eleven"] = , g["twelve"] = , g["thirteen"] = , g["fourteen"] = ;
g["fifteen"] = , g["sixteen"] = , g["seventeen"] = , g["eighteen"] = , g["nineteen"] = ;
g["twenty"] = , g["thirty"] = , g["forty"] = , g["fifty"] = , g["sixty"] = ;
g["seventy"] = , g["eighty"] = , g["ninety"] = ;
} int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, x, y, numCase = ;
init();
cin >> t;
cin.get();//
while(t--){
string ss;
getline(cin, ss, '\n');//
ss += ' ';
int len = ss.length();
int num = , cur = , ans = ;
for(i = ; i < len; ++i){
if(ss[i] == ' '){
string str = ss.substr(num, i - num);
num = i + ;
if(str == "and"){
continue;
} else if(str == "million"){
cur *= ;
ans += cur;
cur = ;
} else if(str == "thousand"){
cur *= ;
ans += cur;
cur = ;
} else if(str == "hundred"){
cur *= ;
} else{
cur += g[str];
}
}
}
ans += cur;
cout << ans << endl;
} return ;
}