您的位置:首页 > 其它

poj 2121 Inglish-Number Translator

2013-12-21 11:28 274 查看
Inglish-Number Translator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4380Accepted: 1703
Description

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English
words that your program must account for:

negative, 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

Input

The input consists of several instances. Notes on input:

Negative numbers will be preceded by the word negative.

The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred".

The input is terminated by an empty line.
Output

The answers are expected to be on separate lines with a newline after each.
Sample Input
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

Sample Output
6
-729
1000101
814022

Source

CTU Open 2004,UVA 486

#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;
map<string, int> dict;

int main()
{
// init
dict["zero"] = 0;
dict["one"] = 1;
dict["two"] = 2;
dict["three"] = 3;
dict["four"] = 4;
dict["five"] = 5;
dict["six"] = 6;
dict["seven"] = 7;
dict["eight"] = 8;
dict["nine"] = 9;
dict["ten"] = 10;
dict["eleven"] = 11;
dict["twelve"] = 12;
dict["thirteen"] = 13;
dict["fourteen"] = 14;
dict["fifteen"] = 15;
dict["sixteen"] = 16;
dict["seventeen"] = 17;
dict["eighteen"] = 18;
dict["nineteen"] = 19;
dict["twenty"] = 20;
dict["thirty"] = 30;
dict["forty"] = 40;
dict["fifty"] = 50;
dict["sixty"] = 60;
dict["seventy"] = 70;
dict["eighty"] = 80;
dict["ninety"] = 90;
dict["hundred"] = 100;
dict["thousand"] = 1000;
dict["million"] = 1000000;

int result, curr, last;
string str;
istringstream iss;
while (getline(cin, str, '\n'))
{
if (str == "")
break;
last = result = 0;
iss.str(str);
iss.clear();
while (iss >> str)
{
if (str == "negative")
{
cout << '-';
continue;
}
curr = dict[str];
if (curr > 100)
{
if (last > curr)
result = result / last * last + (result % last * curr);
else
result *= curr;
last = curr;
}
else if (curr == 100)
{
if (last > 100)
result = result / last * last + (result % last * 100);
else
result *= 100;
}
else
result += curr;
}
cout << result << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: