Inglish-Number Translator
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2822 | Accepted: 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
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:
The input is terminated by an empty line.
- 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; }
该博客主要介绍了如何解决POJ 2121问题,即从英文形式的数字转换为罗马数字。文章中提到,输入的负数会带有“negative”前缀,并且在可以使用“thousand”的情况下不会使用“hundred”。文章未给出具体的解决方案,但预期会包含转换的策略和步骤。
592

被折叠的 条评论
为什么被折叠?



