POJ 2121 Inglish-Number Translator

来源:互联网 发布:mac英文如何设置成中文 编辑:程序博客网 时间:2024/05/18 17:40
Inglish-Number Translator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4840 Accepted: 1894

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: 
  1. Negative numbers will be preceded by the word negative. 
  2. 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

sixnegative seven hundred twenty nineone million one hundred oneeight hundred fourteen thousand twenty two

Sample Output

6-7291000101814022

Source

CTU Open 2004,UVA 486

AC代码:

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;int main(){    string t;    int sum=0,sum2=0;    while(cin>>t){        if(t=="negative")putchar('-');        else if(t=="zero")sum+=0;        else if(t=="one")sum+=1;        else if(t=="two")sum+=2;        else if(t=="three")sum+=3;        else if(t=="four")sum+=4;        else if(t=="five")sum+=5;        else if(t=="six")sum+=6;        else if(t=="seven")sum+=7;        else if(t=="eight")sum+=8;        else if(t=="nine")sum+=9;        else if(t=="ten")sum+=10;        else if(t=="eleven")sum+=11;        else if(t=="twelve")sum+=12;        else if(t=="thirteen")sum+=13;        else if(t=="fourteen")sum+=14;        else if(t=="fifteen")sum+=15;        else if(t=="sixteen")sum+=16;        else if(t=="seventeen")sum+=17;        else if(t=="eighteen")sum+=18;        else if(t=="nineteen")sum+=19;        else if(t=="twenty")sum+=20;        else if(t=="thirty")sum+=30;        else if(t=="forty")sum+=40;        else if(t=="fifty")sum+=50;        else if(t=="sixty")sum+=60;        else if(t=="seventy")sum+=70;        else if(t=="eighty")sum+=80;        else if(t=="ninety")sum+=90;        else if(t=="hundred")sum*=100;        else if(t=="thousand"){            sum2+=sum*1000;            sum=0;        }        else if(t=="million"){            sum2+=sum*1000000;            sum=0;        }        char c;        c=getchar();        if(c=='\n'){            cout<<sum2+sum<<'\12';            sum2=0;            sum=0;        }    }    return 0;}


0 0
原创粉丝点击