您的位置:首页 > 其它

poj2121

2016-07-23 17:14 405 查看
Inglish-Number Translator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5166Accepted: 2030
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:

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


最有杀伤力的一个one million one hundred eleven thousand one hundred eleven

这里要注意的就是

<span style="white-space:pre">	</span>if(input==a[i].str){
if(i==30){
ans+=temp*a[i].num;
temp=0;
}
else if(i==29){
ans+=temp*1000;
temp=0;
}
else if(i==28)
temp*=100;
else
temp+=a[i].num;
break;
}


主要就是叠加的问题

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct number
{
string str;
int num;
}a[40];

void deal()
{
a[0].num=0;a[0].str="zero";
a[1].num=1;a[1].str="one";
a[2].num=2;a[2].str="two";
a[3].num=3;a[3].str="three";
a[4].num=4;a[4].str="four";
a[5].num=5;a[5].str="five";
a[6].num=6;a[6].str="six";
a[7].num=7;a[7].str="seven";
a[8].num=8;a[8].str="eight";
a[9].num=9;a[9].str="nine";
a[10].num=10;a[10].str="ten";
a[11].num=11;a[11].str="eleven";
a[12].num=12;a[12].str="twelve";
a[13].num=13;a[13].str="thirteen";
a[14].num=14;a[14].str="fourteen";
a[15].num=15;a[15].str="fifteen";
a[16].num=16;a[16].str="sixteen";
a[17].num=17;a[17].str="seventeen";
a[18].num=18;a[18].str="eighteen";
a[19].num=19;a[19].str="nineteen";
a[20].num=20;a[20].str="twenty";
a[21].num=30;a[21].str="thirty";
a[22].num=40;a[22].str="forty";
a[23].num=50;a[23].str="fifty";
a[24].num=60;a[24].str="sixty";
a[25].num=70;a[25].str="seventy";
a[26].num=80;a[26].str="eighty";
a[27].num=90;a[27].str="ninety";
a[28].num=100;a[28].str="hundred";
a[29].num=1000;a[29].str="thousand";
a[30].num=1000000;a[30].str="million";
a[31].num=-1;a[31].str="negative";
}

int main()
{
deal();
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

string input;
char ch;
int ans=0,temp=0;
while(cin>>input)
{
if(input==a[31].str)
cout<<'-';
for(int i=0;i<31;i++){
if(input==a[i].str){
if(i==30){
ans+=temp*a[i].num;
temp=0;
}
else if(i==29){
ans+=temp*1000;
temp=0;
}
else if(i==28)
temp*=100;
else
temp+=a[i].num;
break;
}
}
ch=getchar();
if(ch=='\n'){
cout<<ans+temp<<endl;
ans=0;
temp=0;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: