http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1180
poj 2121 http://poj.org/problem?id=2121
题目大意:输入数字的英文写法,输出阿拉伯数字。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <string> 9 #include <set> 10 #include <cstdlib> 11 #include <map> 12 using namespace std; 13 14 map<string, int>my; 15 16 void init() 17 { 18 my["zero"] = 0; 19 my["one"] = 1; 20 my["two"] = 2; 21 my["three"] = 3; 22 my["four"] = 4; 23 my["five"] = 5; 24 my["six"] = 6; 25 my["seven"] = 7; 26 my["eight"] = 8; 27 my["nine"] = 9; 28 my["ten"] = 10; 29 my["eleven"] = 11; 30 my["twelve"] = 12; 31 my["thirteen"] = 13; 32 my["fourteen"] = 14; 33 my["fifteen"] = 15; 34 my["sixteen"] = 16; 35 my["seventeen"] = 17; 36 my["eighteen"] = 18; 37 my["nineteen"] = 19; 38 my["twenty"] = 20; 39 my["thirty"] = 30; 40 my["forty"] = 40; 41 my["fifty"] = 50; 42 my["sixty"] = 60; 43 my["seventy"] = 70; 44 my["eighty"] = 80; 45 my["ninety"] = 90; 46 my["hundred"] = 100; 47 my["thousand"] = 1000; 48 my["million"] = 1000000; 49 } 50 51 string str[200]; 52 int k; 53 54 void solve(string s) 55 { 56 string st = ""; 57 k = 0; 58 for (int i = 0; i < s.size(); i++) 59 { 60 if (s[i] == ' ') 61 { 62 if (st.size()) 63 str[k++] = st; 64 st = ""; 65 } 66 else 67 st += s[i]; 68 } 69 if (st.size()) 70 str[k++] = st; 71 int start = 0; 72 if (str[start] == "negative") 73 { 74 cout << "-"; 75 start++; 76 } 77 int tmp = 0; 78 int sum = 0; 79 for (int i = start; i < k; i++) 80 { 81 if (my[str[i]] == 1000 || my[str[i]] == 1000000) 82 { 83 tmp *= my[str[i]]; 84 sum += tmp; 85 tmp = 0; 86 } 87 else if (my[str[i]] == 100) 88 { 89 tmp *= my[str[i]]; 90 } 91 else 92 tmp += my[str[i]]; 93 } 94 sum += tmp; 95 cout << sum << endl; 96 } 97 98 int main() 99 { 100 string s; 101 init(); 102 while (getline(cin, s)) 103 { 104 if (s.size() == 0 || s.size() == 1) 105 continue; 106 solve(s); 107 } 108 return 0; 109 }