POJ-2121 Inglish-Number Translator-數字英譯漢

kewlgrl發表於2015-08-22
Inglish-Number Translator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4885   Accepted: 1905

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

six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

Sample Output

6
-729
1000101
814022

Source

CTU Open 2004,UVA 486

/*    
*Copyright (c)2015,煙臺大學計算機與控制工程學院    
*All rights reserved.      
*作    者:單昕昕    
*完成日期:2015年8月22日    
*版 本 號:v1.0        
*/ 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

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

這個。。就是直接暴力模擬吧。。還好過了。。

相關文章