zoj 2971 解题思路

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 than109 are considered). Numberabc,def,ghi is written as "[abc] million[def] thousand[ghi]". Here "[xyz] " means the written down numberxyz .

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 ifghi = 0 . If the whole number is equal to0 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 ifx = 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", number100,000,037 as "one hundred million thirty seven", number1,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 integerT (1 <=T <= 1900) which is the number of test cases. It will be followed byT 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 than109.

Sample Input

3
one
eleven
one hundred and two

Sample Output

1
11
102

题目大意:输入一个英文单词,输出其对应的阿拉伯数字。

思路:思路完全错误。。一开始就没有想到怎么去写。。竟然想到搞个数据库来存所有的可能,思想啊,思想。。错就错在想弄个key和value对应的数据结构。枚举所有可能

不太现实。

改正:既然已经想到所有的可能枚举出来了,我一个串一个串进行比较不就行了吗(分治思想),然后个位,十位,百位进行相应的计算。在把结果组合起来。这里又想到

and 这个单词怎么处理。其实想想根本不用理他,扫描字符串的时候只需要找到 hundred ,thousand,million就可以了。

//找的大神的代码。好像ac不了,不过我喜欢这种思路。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[28][15]={"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"} ;
int main()
{   
    int i,n1,n2,n3,N;
    char c,s[100];
    scanf("%d",&N);
	getchar();
        while(N--){
            n1=n2=n3=0;
            while(1){
                scanf("%s",s);
                if(s[0]=='m'){n1=(n2+n3)*1000000;n2=0;n3=0;}
                if(strcmp(s,"thousand")==0){n2=n3*1000;n3=0;}
                   if(strcmp(s,"hundred")==0)   {n3*=100;}
                for(i=0;i<20;i++){
                    if(strcmp(str[i],s)==0){n3+=i;break;}              
                }
                for(i=20;i<28;i++){
                    if(strcmp(str[i],s)==0){n3+=(i-18)*10;break;}               
                }
                c=getchar();
                if(c=='\n') break;
            }  
            printf("%d\n",n1+n2+n3);
        }                      
    
    system("pause");
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值