zoukankan      html  css  js  c++  java
  • POJ 2121 Inglish-Number Translator

    Inglish-Number Translator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4183   Accepted: 1608

    Description

    In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:  negative, 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 

    Input

    The input consists of several instances. Notes on input: 
    1. Negative numbers will be preceded by the word negative. 
    2. The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred".
    The input is terminated by an empty line.

    Output

    The answers are expected to be on separate lines with a newline after each.

    Sample Input

    six
    negative seven hundred twenty nine
    one million one hundred one
    eight hundred fourteen thousand twenty two
    
    

    Sample Output

    6
    -729
    1000101
    814022

    给出一个字符串的英文表示,求出它的阿拉伯数字形式

    我的作法是:先找到英文表示中的million,thousand作为分界线,将数字分为三段,再分别分析每一段的阿拉伯数字表示即可

     

      1 #include<stdio.h>
      2 #include<string.h>
      3 
      4 int million_pos,thousand_pos,million,thousand,one;
      5 char s[10000],word[100][20];
      6 
      7 int find_num(int x,int y)
      8 {
      9     int i,ans=0,hundred_pos=-1;
     10 
     11     for(i=x;i<=y;i++)
     12         if(!strcmp(word[i],"hundred"))
     13         {
     14             hundred_pos=i;
     15             break;
     16         }
     17 
     18     if(hundred_pos==-1)
     19     {
     20         for(i=x;i<=y;i++)
     21             if(!strcmp(word[i],"one"))
     22                 ans+=1;
     23             else if(!strcmp(word[i],"two"))
     24                 ans+=2;
     25             else if(!strcmp(word[i],"three"))
     26                 ans+=3;
     27             else if(!strcmp(word[i],"four"))
     28                 ans+=4;
     29             else if(!strcmp(word[i],"five"))
     30                 ans+=5;
     31             else if(!strcmp(word[i],"six"))
     32                 ans+=6;
     33             else if(!strcmp(word[i],"seven"))
     34                 ans+=7;
     35             else if(!strcmp(word[i],"eight"))
     36                 ans+=8;
     37             else if(!strcmp(word[i],"nine"))
     38                 ans+=9;
     39             else if(!strcmp(word[i],"ten"))
     40                 ans+=10;
     41             else if(!strcmp(word[i],"eleven"))
     42                 ans+=11;
     43             else if(!strcmp(word[i],"twelve"))
     44                 ans+=12;
     45             else if(!strcmp(word[i],"thirteen"))
     46                 ans+=13;
     47             else if(!strcmp(word[i],"fourteen"))
     48                 ans+=14;
     49             else if(!strcmp(word[i],"fifteen"))
     50                 ans+=15;
     51             else if(!strcmp(word[i],"sixteen"))
     52                 ans+=16;
     53             else if(!strcmp(word[i],"seventeen"))
     54                 ans+=17;
     55             else if(!strcmp(word[i],"eighteen"))
     56                 ans+=18;
     57             else if(!strcmp(word[i],"nineteen"))
     58                 ans+=19;
     59             else if(!strcmp(word[i],"twenty"))
     60                 ans+=20;
     61             else if(!strcmp(word[i],"thirty"))
     62                 ans+=30;
     63             else if(!strcmp(word[i],"forty"))
     64                 ans+=40;
     65             else if(!strcmp(word[i],"fifty"))
     66                 ans+=50;
     67             else if(!strcmp(word[i],"sixty"))
     68                 ans+=60;
     69             else if(!strcmp(word[i],"seventy"))
     70                 ans+=70;
     71             else if(!strcmp(word[i],"eighty"))
     72                 ans+=80;
     73             else if(!strcmp(word[i],"ninety"))
     74                 ans+=90;
     75     }
     76     else
     77     {
     78         for(i=x;i<hundred_pos;i++)
     79             if(!strcmp(word[i],"one"))
     80                 ans+=100;
     81             else if(!strcmp(word[i],"two"))
     82                 ans+=200;
     83             else if(!strcmp(word[i],"three"))
     84                 ans+=300;
     85             else if(!strcmp(word[i],"four"))
     86                 ans+=400;
     87             else if(!strcmp(word[i],"five"))
     88                 ans+=500;
     89             else if(!strcmp(word[i],"six"))
     90                 ans+=600;
     91             else if(!strcmp(word[i],"seven"))
     92                 ans+=700;
     93             else if(!strcmp(word[i],"eight"))
     94                 ans+=800;
     95             else if(!strcmp(word[i],"nine"))
     96                 ans+=900;
     97         for(i=hundred_pos+1;i<=y;i++)
     98             if(!strcmp(word[i],"one"))
     99                 ans+=1;
    100             else if(!strcmp(word[i],"two"))
    101                 ans+=2;
    102             else if(!strcmp(word[i],"three"))
    103                 ans+=3;
    104             else if(!strcmp(word[i],"four"))
    105                 ans+=4;
    106             else if(!strcmp(word[i],"five"))
    107                 ans+=5;
    108             else if(!strcmp(word[i],"six"))
    109                 ans+=6;
    110             else if(!strcmp(word[i],"seven"))
    111                 ans+=7;
    112             else if(!strcmp(word[i],"eight"))
    113                 ans+=8;
    114             else if(!strcmp(word[i],"nine"))
    115                 ans+=9;
    116             else if(!strcmp(word[i],"ten"))
    117                 ans+=10;
    118             else if(!strcmp(word[i],"eleven"))
    119                 ans+=11;
    120             else if(!strcmp(word[i],"twelve"))
    121                 ans+=12;
    122             else if(!strcmp(word[i],"thirteen"))
    123                 ans+=13;
    124             else if(!strcmp(word[i],"fourteen"))
    125                 ans+=14;
    126             else if(!strcmp(word[i],"fifteen"))
    127                 ans+=15;
    128             else if(!strcmp(word[i],"sixteen"))
    129                 ans+=16;
    130             else if(!strcmp(word[i],"seventeen"))
    131                 ans+=17;
    132             else if(!strcmp(word[i],"eighteen"))
    133                 ans+=18;
    134             else if(!strcmp(word[i],"nineteen"))
    135                 ans+=19;
    136             else if(!strcmp(word[i],"twenty"))
    137                 ans+=20;
    138             else if(!strcmp(word[i],"thirty"))
    139                 ans+=30;
    140             else if(!strcmp(word[i],"forty"))
    141                 ans+=40;
    142             else if(!strcmp(word[i],"fifty"))
    143                 ans+=50;
    144             else if(!strcmp(word[i],"sixty"))
    145                 ans+=60;
    146             else if(!strcmp(word[i],"seventy"))
    147                 ans+=70;
    148             else if(!strcmp(word[i],"eighty"))
    149                 ans+=80;
    150             else if(!strcmp(word[i],"ninety"))
    151                 ans+=90;
    152     }
    153 
    154     return ans;
    155 }
    156 
    157 int main()
    158 {
    159     int i,t;
    160 
    161     while(gets(s))
    162     {
    163         if(s[0]=='')
    164             break;
    165 
    166         t=0;
    167         for(i=0;s[i]!='';i+=strlen(word[t-1]))
    168             sscanf(&s[i],"%s",word[t++]);
    169 
    170         million_pos=thousand_pos=-1;
    171         for(i=0;i<t;i++)
    172             if(!strcmp(word[i],"million"))
    173                 million_pos=i;
    174             else if(!strcmp(word[i],"thousand"))
    175                 thousand_pos=i;
    176 
    177         i=0;
    178         if(!strcmp(word[i],"negative"))
    179         {
    180             printf("-");
    181             i++;
    182         }
    183 
    184         if(million_pos==-1)
    185         {
    186             if(thousand_pos==-1)
    187                 printf("%d
    ",find_num(i,t-1));
    188             else
    189             {
    190                 printf("%d",find_num(i,thousand_pos-1));
    191                 printf("%03d
    ",find_num(thousand_pos+1,t-1));
    192             }
    193         }
    194         else
    195         {
    196             if(thousand_pos==-1)
    197             {
    198                 printf("%d",find_num(i,million_pos-1));
    199                 printf("000");
    200                 printf("%03d
    ",find_num(million_pos+1,t-1));
    201             }
    202             else
    203             {
    204                 printf("%d",find_num(i,million_pos-1));
    205                 printf("%03d",find_num(million_pos+1,thousand_pos-1));
    206                 printf("%03d
    ",find_num(thousand_pos+1,t-1));
    207             }
    208         }
    209     }
    210 
    211     return 0;
    212 }
    [C]

     

  • 相关阅读:
    C#读取Excel日期时间
    软件需求3个层次――业务需求、用户需求和功能需求
    软件开发中的基线
    软件开发过程(CMMI/RUP/XP/MSF)是与非
    第1章项目初始.pdf
    计算机鼓轮
    概念模型,逻辑模型,物理模型
    第0章项目管理概述.pdf
    C#中提供的精准测试程序运行时间的类Stopwatch
    Installing and configuring OpenSSH with pam_ldap for RedHat Enterprise Linux3
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3222610.html
Copyright © 2011-2022 走看看