ZOJ2971

    xiaoxiao2021-04-03  47

    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

     

    个人感觉中等难的一道模拟题;输入用英文表达的一个数,让你转化成阿拉伯数字输出。

     

    如果是一个上千万的数, 则有 A,B,C三个部分,(A是千万的那部分),可以发现每一部分都同样要先表达1000以下的数,再根据大小再在后面添上millio或者thousand

    ,所以我们可以先把前面小于1000的数存上,再判断后面有没有milli等单位。因为没输入一个单词都有空格,所以可以用空格来判断。

     

    #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define clr(x) memset(x, 0, sizeof(x)) #define INF 1e9 using namespace std; char trans[100][20]={"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", "and"}; int main() { int T; char str[2000]; char tmp[100]; scanf("%d", &T); getchar(); while(T--) { clr(str); clr(tmp); gets(str); int cnt = 0; int all = 0; int sum = 0; for(int i=0; i<=strlen(str); i++) { if(str[i] != ' ' && str[i] != '\0') { tmp[cnt++] = str[i]; } else if(str[i] == ' ' || str[i] == '\0') { int mid = INF; for(int k = 0; k <= 30; k++) { if(!strcmp(tmp, trans[k])) { mid = k; break; } } if(mid <= 20) all += mid; else if(mid > 20 && mid <= 27) all += (mid - 20 + 2) * 10; else if(mid == 28) { all *= 100; } else if(mid == 29) { sum += all * 1000; all = 0; } else if(mid == 30) { sum += all * 1000000; all = 0; } clr(tmp); cnt = 0; } } printf("%d\n", sum + all); } return 0; }

     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-666027.html

    最新回复(0)