http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971

开始想用map的键值对来做,想想发现没必要,直接模拟就可以了。用一个数组打出0—20和30,40..90等数的英文,输入时进行匹配,匹配成功便通过它们与下标之间的关系确定。输入为million,hundred,thousand时,当前值要乘相应10^n。

用三个数n1, n2, n3分别记录每三位数,最后相加即可。

code:

#include<cstdio>
#include<cstring>
using namespace std ;
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"} ;
char s[15] ;
int main(){
    int t, i, j, n1, n2, n3, f ;
    char c = 0 ;
    scanf("%d", &t) ;
    getchar() ;
    while(t--){
        n1 = n2 = n3 = 0 ;
        while(1){
            scanf("%s", s) ;
            f = 0 ;
            if(s[0]=='m'){
                n1 = (n2 + n3) * 1000000 ;
                n2 = n3 = 0 ;
                f = 1 ;
            }
            if(strcmp(s, "thousand")==0){
                n2 = n3 * 1000 ;
                n3 = 0 ;
                f = 1 ;
            }
            if(s[0]=='h') n3 *= 100, f = 1 ;
            for(i=0; i<=20; i++){
                if(strcmp(s, str[i])==0){
                    n3 += i ;
                    break ;
                }
            }
            for(i=21; i<28; i++){
                if(strcmp(s, str[i])==0){
                    n3 += (i - 18) * 10 ;
                    break ;
                }
            }
            c = getchar() ;
            if(c=='\n'break ;
        }
        printf("%d\n", n1+n2+n3) ;
    }
    return 0 ;
}

posted on 2012-04-28 22:03  追逐.  阅读(429)  评论(0编辑  收藏  举报