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

    技术2022-05-20  57

    Inglish-Number Translator Time Limit: 1000MS Memory Limit: 65536KTotal 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

    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; }

    最新回复(0)