Inglish-Number Translator
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5162 | Accepted: 2027 |
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
C++string类写 #include <iostream> #include <string> #include <cstdio> using namespace std; string a[100]={"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"}; int main(){ string s; int ans=0,sum=0; while(cin>>s){ if(s==a[0]) cout<<"-"; else if(s==a[1]) sum+=0; else if(s==a[2]) sum+=1; else if(s==a[3]) sum+=2; else if(s==a[4]) sum+=3; else if(s==a[5]) sum+=4; else if(s==a[6]) sum+=5; else if(s==a[7]) sum+=6; else if(s==a[8]) sum+=7; else if(s==a[9]) sum+=8; else if(s==a[10]) sum+=9; else if(s==a[11]) sum+=10; else if(s==a[12]) sum+=11; else if(s==a[13]) sum+=12; else if(s==a[14]) sum+=13; else if(s==a[15]) sum+=14; else if(s==a[16]) sum+=15; else if(s==a[17]) sum+=16; else if(s==a[18]) sum+=17; else if(s==a[19]) sum+=18; else if(s==a[20]) sum+=19; else if(s==a[21]) sum+=20; else if(s==a[22]) sum+=30; else if(s==a[23]) sum+=40; else if(s==a[24]) sum+=50; else if(s==a[25]) sum+=60; else if(s==a[26]) sum+=70; else if(s==a[27]) sum+=80; else if(s==a[28]) sum+=90; else if(s==a[29]) sum*=100; else if(s==a[30]) { ans+=sum*1000; sum=0; } else if(s==a[31]) { ans+=sum*1000000; sum=0; } char ch = getchar(); if(ch=='\n'){ cout<<sum+ans<<endl; sum=0; ans=0; } } return 0; }
C的strcmp写 #include<iostream> #include<cstdio> #include<cstring> #define maxn 100 char s[maxn]; int sum=0,ans=0; int main() { while(scanf("%s",s)!=EOF) { if(!strcmp(s,"negative"))printf("-"); else if(!strcmp(s,"zero"))sum+=0; else if(!strcmp(s,"one"))sum+=1; else if(!strcmp(s,"two"))sum+=2; else if(!strcmp(s,"three"))sum+=3; else if(!strcmp(s,"four"))sum+=4; else if(!strcmp(s,"five"))sum+=5; else if(!strcmp(s,"six"))sum+=6; else if(!strcmp(s,"seven"))sum+=7; else if(!strcmp(s,"eight"))sum+=8; else if(!strcmp(s,"nine"))sum+=9; else if(!strcmp(s,"ten"))sum+=10; else if(!strcmp(s,"eleven"))sum+=11; else if(!strcmp(s,"twelve"))sum+=12; else if(!strcmp(s,"thirteen"))sum+=13; else if(!strcmp(s,"fourteen"))sum+=14; else if(!strcmp(s,"fifteen"))sum+=15; else if(!strcmp(s,"sixteen"))sum+=16; else if(!strcmp(s,"seventeen"))sum+=17; else if(!strcmp(s,"eighteen"))sum+=18; else if(!strcmp(s,"nineteen"))sum+=19; else if(!strcmp(s,"twenty"))sum+=20; else if(!strcmp(s,"thirty"))sum+=30; else if(!strcmp(s,"forty"))sum+=40; else if(!strcmp(s,"fifty"))sum+=50; else if(!strcmp(s,"sixty"))sum+=60; else if(!strcmp(s,"seventy"))sum+=70; else if(!strcmp(s,"eighty"))sum+=80; else if(!strcmp(s,"ninety"))sum+=90; else if(!strcmp(s,"hundred")) { sum*=100; } else if(!strcmp(s,"thousand")) { ans+=sum*1000;<pre><span style="color:#008000;">//</span><span style="color:#008000;">当为1000之后,原数应加上num*1000,而非直接乘,1000000也是一样</span> sum=0; } else if(!strcmp(s,"million")) { ans+=sum*1000000; sum=0; } //当系统读到回车时,输出 char ch=getchar(); if(ch=='\n') { printf("%d\n",ans+sum); sum=0; ans=0; } } return 0; }
来源:https://www.cnblogs.com/mingrigongchang/p/6246284.html