您的位置:首页 > 其它

poj 2121 Inglish-Number Translator 将英文翻译成罗马数字

2011-02-25 23:24 471 查看
Inglish-Number Translator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 2822Accepted: 1107
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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
string sig="negative";
string tmp[31]={"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 position(string str)
{
for(int i=0;i<31;i++) if(tmp[i]==str) return i;
}
int val(int n)
{
if(n>=20&&n<=27) return (n-18)*10;
else if(n==28) return 100;
else if(n==29) return 1000;
else if(n==30) return 1000000;
return n;
}
int main()
{
string s;
while(getline(cin,s),s!="")
{
istringstream sin(s);
string str[100];
int len=0;
while(sin>>str[len]) len++;
int cnt=0,tmp=0;
//不会出现one thousand millon 这种情况
for(int i=0;i<=len;i++)
{
if(i==len) {cnt+=tmp;break;}
if(str[i]==sig) {printf("-");continue;}
int tag=val(position(str[i]));
if(tag==100) tmp*=tag;
else if(tag>100) tmp*=tag,cnt+=tmp,tmp=0;
else tmp+=tag;
}
printf("%d/n",cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: