码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2121 Inglish-Number Translator

时间:2015-08-14 13:44:15      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:c++   poj   iostream   编程   

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

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

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2121 Inglish-Number Translator

标签:c++   poj   iostream   编程   

原文地址:http://blog.csdn.net/zp___waj/article/details/47660155

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!