Inglish-Number Translator
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5247 | Accepted: 2064 |
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
Source
CTU Open 2004,UVA 486
题意:给定一行英文数字将其转化为阿拉伯数字表示。
解:模拟,使用
cin输入字符串以空格结束,判断每个字符串,采取相应做法。定义sum,以及sum1。遇到
thousand,million的时候,
sum1=sum1+sum*1000(1000000)而此时sum应置为0;遇到hundred时,sum=sum*100,其余情况都是采取加法。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
string a;
long long sum=0;
long long sum1=0;
while(cin>>a){
if(a=="negative")
printf("-");
else if(a=="zero")
sum+=0;
else if(a=="one")
sum+=1;
else if(a=="two")
sum+=2;
else if(a=="three")
sum+=3;
else if(a=="four")
sum+=4;
else if(a=="five")
sum+=5;
else if(a=="six")
sum+=6;
else if(a=="seven")
sum+=7;
else if(a=="eight")
sum+=8;
else if(a=="nine")
sum+=9;
else if(a=="ten")
sum+=10;
else if(a=="eleven")
sum+=11;
else if(a=="twelve")
sum+=12;
else if(a=="thirteen")
sum+=13;
else if(a=="fourteen")
sum+=14;
else if(a=="fifteen")
sum+=15;
else if(a=="sixteen")
sum+=16;
else if(a=="seventeen")
sum+=17;
else if(a=="eighteen")
sum+=18;
else if(a=="nineteen")
sum+=19;
else if(a=="twenty")
sum+=20;
else if(a=="thirty")
sum+=30;
else if(a=="forty")
sum+=40;
else if(a=="fifty")
sum+=50;
else if(a=="sixty")
sum+=60;
else if(a=="seventy")
sum+=70;
else if(a=="eighty")
sum+=80;
else if(a=="ninety")
sum+=90;
else if(a=="hundred")
sum=sum*100;
else if(a=="thousand"){
sum1=sum1+sum*1000;
sum=0;
}else if(a=="million"){
sum1=sum1+sum*1000000;
sum=0;
}
if(getchar()=='\n'){
printf("%I64d\n",sum1+sum);
sum1=sum=0;
}
}
return 0;
}