Inglish-Number Translator

来源:互联网 发布:java 限制访问次数 编辑:程序博客网 时间:2024/05/18 20:12

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

sixnegative seven hundred twenty nineone million one hundred oneeight hundred fourteen thousand twenty two

Sample Output

给出一个字符串,输出它的数值形式

6-7291000101

814022

错误代码:
/*#include <cstdio>#include <iostream>#include <string>#include <map>#include <algorithm>using namespace std;int main(){    string str;    map<string,int> map1;    map1["one"]=1;    map1["negative"]=-1;    map1["two"]=2;    map1["three"]=3;    map1["four"]=4;    map1["five"]=5;    map1["six"]=6;    map1["seven"]=7;    map1["eight"]=8;    map1["nine"]=9;    map1["ten"]=10;    map1["eleven"]=11;    map1["twelve"]=12;    map1["thirteen"]=13;    map1["fourteen"]=14;    map1["fifteen"]=15;    map1["sixteen"]=16;    map1["seventeen"]=17;    map1["eighteen"]=18;    map1["nineteen"]=19;    map1["twenty"]=20;    map1["thirty"]=30;    map1["forty"]=40;    map1["fifty"]=50;    map1["sixty"]=60;    map1["seventy"]=70;    map1["eighty"]=80;    map1["ninety"]=90;    map1["hundred"]=100;    map1["thousand"]=1000;    map1["million"]=1000000;    while(getline(cin,str))    {        if(str=="")            break;        string str1="";        int len=str.length();        int i,flag=1,ans=0,sum=0,start=0;        for(i=0; i<len;)        {            while(i<len)            {                if(str[i]==' ')                {                    str1=str.substr(start,i-start);                    start=++i;                    if(map1[str1]==-1)                        flag=-1;                    else if(str1=="hundred"||str1=="thousand"||str1=="million")                    {                        if(ans<=map1[str1])                            ans=ans*map1[str1];                        sum=sum*map1[str1];                        ans+=sum;                        sum=0;                    }                    else                    {                        sum=sum+map1[str1];                    }                    break;                }                i++;            }            //if(sum==720)            //cout<<str1<<endl;        }        str1=str.substr(start,i-start);        //cout<<sum<<endl;        if(str1=="hundred"||str1=="thousand"||str1=="million")        {            if(ans<=map1[str1])                ans=ans*map1[str1];            sum=sum*map1[str1];        }        else        {            sum=sum+map1[str1];        }        ans+=sum;        printf("%d\n",flag*ans);    }    return 0;}*/
AC代码:#include<iostream>#include<string>#include<map>using namespace std;int main(){    map<string ,long long > map1;    map1["one"]=1;    map1["negative"]=-1;    map1["two"]=2;    map1["three"]=3;    map1["four"]=4;    map1["five"]=5;    map1["six"]=6;    map1["seven"]=7;    map1["eight"]=8;    map1["nine"]=9;    map1["ten"]=10;    map1["eleven"]=11;    map1["twelve"]=12;    map1["thirteen"]=13;    map1["fourteen"]=14;    map1["fifteen"]=15;    map1["sixteen"]=16;    map1["seventeen"]=17;    map1["eighteen"]=18;    map1["nineteen"]=19;    map1["twenty"]=20;    map1["thirty"]=30;    map1["forty"]=40;    map1["fifty"]=50;    map1["sixty"]=60;    map1["seventy"]=70;    map1["eighty"]=80;    map1["ninety"]=90;    map1["hundred"]=100;    map1["thousand"]=1000;    map1["million"]=1000000;    long long temp,now,r;    string str,num;    while(getline(cin,str))    {        if(str=="")            break;        long long sign=1,temp=0,r=0,now=0;        long long start=0;        long long L=str.size();        for(long long i=start; i<=L; i++)        {            if(str[i]==' '||i==L)            {                num=str.substr(start,i-start);                now=map1[num];                if(now==-1)                    sign=now;                else                {                    if(now>0&&now<100)                        temp+=now;                    else                    {                        if(now==100)                            temp*=100;                        else                        {                            r+=temp*now;                            temp=0;                        }                    }                }                start=i+1;            }        }        r+=temp;        cout<<sign*r<<endl;    }    return 0;}


0 0