zoukankan      html  css  js  c++  java
  • ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)

    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
    

    Author: CAO, Peng
    Source: The 5th Zhejiang Provincial Collegiate Programming Contest

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    using namespace std;
    map<string,int> mp;
    char ch[100],str[10];
    int t,ans,num;
    int main()
    {
    
        mp.clear();
        mp["zero"]=0; mp["one"]=1;
        mp["two"]=2; mp["three"]=3;mp["four"]=4;
        mp["five"]=5;mp["six"]=6;mp["seven"]=7; mp["eight"]=8;
        mp["nine"]=9;mp["ten"]=10; mp["eleven"]=11;
        mp["twelve"]=12;mp["thirteen"]=13; mp["fourteen"]=14;
        mp["fifteen"]=15; mp["sixteen"]=16;  mp["seventeen"]=17;
        mp["eighteen"]=18;mp["nineteen"]=19;
        mp["twenty"]=20;  mp["thirty"]=30;  mp["forty"]=40;
        mp["fifty"]=50;  mp["sixty"]=60; mp["seventy"]=70;
        mp["eighty"]=80;  mp["ninety"]=90;
    
        while(~scanf("%d",&t))
        {
            getchar();
        for(;t>0;t--)
        {
            gets(ch);
            //getchar();
            int len=strlen(ch),l=-1;
            ch[len]=' '; len++;
            memset(str,0,10);
            ans=0; num=0;
            for(int i=0;i<len;i++)
            {
              if (ch[i]!=' ') str[++l]=ch[i];
               else
               {
                  if (mp.find(str)!=mp.end()) num+=mp[str];
                  else
                  {
                      if (strcmp(str,"million")==0)
                      {num=num*1000000; ans+=num; num=0;}
                      if (strcmp(str,"thousand")==0)
                      {num=num*1000; ans+=num; num=0;}
                      if (strcmp(str,"hundred")==0)
                       num=num*100;
                  }
                  //str[]='';
                    memset(str,0,10);//把字符串数组清空的神奇方法
                  l=-1;
               }
    
            }
            printf("%d
    ",ans+num);
        }
        }
        return 0;
    }
  • 相关阅读:
    mybatis 从数据库查询的信息不完整解决办法
    Mybatis关联查询,查询出的记录数量与数据库直接查询不一致,如何解决?
    UltraEdit快捷键大全-UltraEdit常用快捷键大全
    使用UltraEdit 替换解决---文字中含有逗号的文件,如何把逗号自动转换成为:回车换行呢?
    在对文件进行随机读写,RandomAccessFile类,如何提高其效率
    雷军:曾经干掉山寨机,现在干掉山寨店(将性价比进行到底)
    tbox的项目:vm86(汇编语言虚拟机),tbox(类似dlib),gbox(c语言实现的多平台图形库)
    Delphi XE8 iOS与Android移动应用开发(APP开发)教程[完整中文版]
    人和动物的最大区别,在于能否控制自己
    CWnd和HWND的区别(hWnd只是CWnd对象的一个成员变量,代表与这个对象绑定的窗口)
  • 原文地址:https://www.cnblogs.com/stepping/p/6406705.html
Copyright © 2011-2022 走看看