首页 > 其他 > 详细

ZOJ2971英文转换数字

时间:2014-03-19 03:35:42      阅读:362      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
Give Me the Number
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Numbers in English are written down in the following way (only numbers less than 109 are considered). Number abc,def,ghi is written as "[abc] million [def] thousand [ghi]". Here "[xyz] " means the written down number xyz .

In the written down number the part "[abc] million" is omitted if abc = 0 , "[def] thousand" is omitted if def = 0 , and "[ghi] " is omitted if ghi = 0 . If the whole number is equal to 0 it is written down as "zero". Note that words "million" and "thousand" are singular even if the number of millions or thousands respectively is greater than one.

Numbers under one thousand are written down in the following way. The number xyz is written as "[x] hundred and [yz] ”. ( If yz = 0 it should be only “[x] hundred”. Otherwise if y = 0 it should be only “[x] hundred and [z]”.) Here "[x] hundred and" is omitted if x = 0 . Note that "hundred" is also always singular.

Numbers under 20 are written down as "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", and "nineteen" respectively. Numbers from 20 to 99 are written down in the following way. Number xy is written as "[x0] [y] ", and numbers divisible by ten are written as "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", and "ninety" respectively.

For example, number 987,654,312 is written down as "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve", number 100,000,037 as "one hundred million thirty seven", number 1,000 as "one thousand". Note that "one" is never omitted for millions, thousands and hundreds.

Give you the written down words of a number, please give out the original number.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1900) which is the number of test cases. It will be followed by T consecutive test cases.

Each test case contains only one line consisting of a sequence of English words representing a number.

Output

For each line of the English words output the corresponding integer in a single line. You can assume that the integer is smaller than 109.

Sample Input

3
one
eleven
one hundred and two

Sample Output

1
11
102
View Question
这题写了半个晚上还不会写...记录两个解法回去看
bubuko.com,布布扣
#include<iostream>
#include<map>
#include<string>
using namespace std;

map<string,int > a;
string s;   //类型
void init()
{
 a["zero"]=0;
 a["one"]=1;
 a["two"]=2;
 a["three"]=3;
 a["four"]=4;
 a["five"]=5;
 a["six"]=6;
 a["seven"]=7;
 a["eight"]=8;
 a["nine"]=9;
 a["ten"]=10;
 a["eleven"]=11;
 a["twelve"]=12;
 a["thirteen"]=13;
 a["fourteen"]=14;
 a["fifteen"]=15;
 a["sixteen"]=16;
 a["seventeen"]=17;
 a["eighteen"]=18;
 a["nineteen"]=19;
 a["twenty"]=20;
 a["thirty"]=30;
 a["forty"]=40;
 a["fifty"]=50;
 a["sixty"]=60;
 a["seventy"]=70;
 a["eighty"]=80;
 a["ninety"]=90;
}
int main()
{
 int t,i;
 init();
 cin>>t;
 cin.get();
 while(t--)
 { 
  getline(cin,s,\n);  
  s+= ;
  int len=s.length();
  int b=0,num=0,sum=0;
  for(i=0;i<len;i++)
  {
   if(s[i]== )
   {
    string str=s.substr(b,i-b);
    b=i+1;
    if(str=="and")
     continue;
    else if(str=="million")
    {
     num*=1000000;
     sum+=num;
     num=0;
    }
    else if(str=="thousand")
    {
     num*=1000;
     sum+=num;
     num=0;
    }
    else if(str=="hundred")
    {
     num*=100;     
    }
    else
     num+=a[str];
   }
  }
  sum+=num;
  cout<<sum<<endl;
 }
 return 0;
}
MAP STL
bubuko.com,布布扣
#include <stdio.h>
int main(void)
{
    char a[31][10] = {"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",  "million", "thousand", "hundred"};
    char ch[200], t[10];
    int T, len, i, j, cnt, now, s;
    
    scanf("%d", &T);
    getchar();
    while(T--)
    {
        s = 0;
        
        gets(ch);
        len = strlen(ch);
        
        for(i = 0; i < len; i++)
        {
            cnt = 0;
            while (ch[i] !=   && ch[i] != \0)
            {
                t[cnt] = ch[i];
                cnt++;
                i++;
            }
            t[cnt] = \0;
            
            for(j = 0; j < 31; j++)
                if(strcmp(t, a[j]) == 0) break;
                
            if(j <= 20)
                now = j;
            else if(j <= 27)
                now = (j - 18)*10;
            else if(j == 28) now = 1000000;
            else if(j == 29) now = 1000;
            else if(j == 30) now = 100;
            else now = -1;

            if(now >- 0)
            {
                if(now < 100) s += now;
                else s = s - s % now + s % now * now;
            }
        }
        printf("%d\n", s);
    }
    return 0;
}
View Code

 

ZOJ2971英文转换数字,布布扣,bubuko.com

ZOJ2971英文转换数字

原文:http://www.cnblogs.com/wushuaiyi/p/3608716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!