2014ACM集训13级PK赛5-Give Me the Number

来源:互联网 发布:安卓必备软件 编辑:程序博客网 时间:2024/05/29 10:49

Description

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 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 ifghi = 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 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", 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 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

3oneelevenone hundred and two

Sample Output

111102

 

 

模拟题。还好英语训练课没逃,刚讲过。

#include <string.h>#include <stdio.h>#include <math.h>#include <stdlib.h>char n[21][20] = {"zero", "one", "two", "three", "four",                "five", "six", "seven", "eight", "nine",                 "ten", "eleven", "twelve", "thirteen",                  "fourteen", "fifteen", "sixteen",                  "seventeen", "eighteen","nineteen"};char ershi[11][20] = {"\0","\0","twenty", "thirty",                    "forty", "fifty","sixty", "seventy",                     "eighty","ninety"};int main(){    int N;    scanf ("%d%*c",&N);    while (N--)    {        char s[100];        long long num = 0;        long long ans = 0;        int i;        int tf = 1;        char c;        while (scanf ("%s%c",s,&c))        {            if (!strcmp (s,"and"))                continue;            for (i = 0;i <= 10;i++)                if (!strcmp (s,ershi[i]))                    num += i * 10;            for (i = 0;i <= 20;i++)                if (!strcmp (s,n[i]))                    num += i;            if (!strcmp (s,"million"))            {                num *= 1000000;                ans += num;                num = 0;            }else if (!strcmp (s,"thousand"))            {                num *= 1000;                ans += num;                num = 0;            }else if (!strcmp (s,"hundred"))                num *= 100;            if (c == '\n')                break;        }        ans += num;        printf ("%lld\n",ans);    }    return 0;}


 

0 0
原创粉丝点击