http://poj.org/problem?id=2121
2.Idea
Calculate digits for three parts of million, thousand, unit.
3.Source
string words[] =
{ "zero", "one", "two", "three",
"four", "five", "six", "seven",
"eight","nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen", "twenty",
"thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety","hundred", "thousand", "million" };
int num2str[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,1000000 };
int main()
{
map<string, int> mp;
for (int i = 0; i < 31; i++) {
mp[words[i]] = num2str[i];
}
string str, line;
while (getline(cin, line)) {
if (line.length() == 0) break;
istringstream input(line);
bool neg = 0;
ll ans = 0, pre = 0;
while (input >> str) {
if (str == "negative") neg = 1;
else if (str == "hundred") pre *= 100;
else if (str == "thousand") {
pre *= 1000;
ans += pre;
pre = 0;
}
else if (str == "million") {
pre *= 1000000;
ans += pre;
pre = 0;
}
else {
pre += mp[str];
}
}
ans += pre;
if (neg) ans *= -1;
cout << ans << endl;
}
return 0;
}
No comments:
Post a Comment