zoukankan      html  css  js  c++  java
  • zoj 2971 Give Me the Number

    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 ;
    }

  • 相关阅读:
    Git 实战教程
    Github 快速上手实战教程
    02.制作一个自己的 Java 编辑器
    Eclipse代码自动提示设置
    java.lang.SuppressWarnings的注解简介
    01.Java 开发简单的计算器
    10个相见恨晚的 Java 在线练手项目
    git 1
    cocos3.2 搭建开发环境
    love 音效
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2475756.html
Copyright © 2011-2022 走看看