poj 2121 Inglish-Number Translator 将英文翻译成罗马数字
2011-02-25 23:24
471 查看
Inglish-Number Translator
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2822 | Accepted: 1107 |
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; }
相关文章推荐
- (字符串的模式匹配4.7.12)POJ 2121 Inglish-Number Translator(将英文数字转换成阿拉伯数字)
- poj 2121 字符串(英文数字->阿拉伯数字)
- POJ-2121 Inglish-Number Translator-数字英译汉
- Java实现将数字日期翻译成英文单词的工具类实例
- 华为上机英文数字翻译
- poj 2121 Inglish-Number Translator
- 趣味程序(将阿拉伯数字翻译为罗马数字)
- 翻译1-5的数字的英文
- poj 2121 Inglish-Number Translator
- POJ 2121 Inglish-Number Translator
- POJ2121——Inglish-Number Translator
- poj 2121 Inglish-Number Translator(模拟)
- 将阿拉伯数字 翻译为 罗马数字
- 英文数字写法翻译阿拉伯数字 [java实现]
- 电话号码对应的英文单词 手机数字短信翻译小工具 数字输入法初型 需求“ya tou”咋写程序实现翻译成“丫头”,有哪位有思路的请指教。
- POJ 2121 Inglish-Number Translator
- POJ2121 ZOJ2311 Inglish-Number Translator【map+字符串比较+水题】
- 数字的翻译(英文到中文)
- 一个把数字日期翻译成英文单词的工具类
- 数字翻译成英文的小游戏