poj2121(字符串)

http://poj.org/problem?id=2121

Inglish-Number Translator
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 3671Accepted: 1460

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

Source

题意:把英文的数字翻译成阿拉伯数字。
想用数组把对应的英文字母保存起来但是不熟练,看到一大牛直接爆,那我也就直接爆就是了
关键的地方是处理 hundred, thousand, million 。自己用笔模拟下就有感觉了。。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100
char s[maxn];
int sum=0,ans=0;
int main()
{
  while(scanf("%s",s)!=EOF)
  {
    if(!strcmp(s,"negative"))printf("-");
    else if(!strcmp(s,"zero"))sum+=0;
    else if(!strcmp(s,"one"))sum+=1;
    else if(!strcmp(s,"two"))sum+=2;
    else if(!strcmp(s,"three"))sum+=3;
    else if(!strcmp(s,"four"))sum+=4;
    else if(!strcmp(s,"five"))sum+=5;
    else if(!strcmp(s,"six"))sum+=6;
    else if(!strcmp(s,"seven"))sum+=7;
    else if(!strcmp(s,"eight"))sum+=8;
    else if(!strcmp(s,"nine"))sum+=9;
    else if(!strcmp(s,"ten"))sum+=10;
    else if(!strcmp(s,"eleven"))sum+=11;
    else if(!strcmp(s,"twelve"))sum+=12;
    else if(!strcmp(s,"thirteen"))sum+=13;
    else if(!strcmp(s,"fourteen"))sum+=14;
    else if(!strcmp(s,"fifteen"))sum+=15;
    else if(!strcmp(s,"sixteen"))sum+=16;
    else if(!strcmp(s,"seventeen"))sum+=17;
    else if(!strcmp(s,"eighteen"))sum+=18;
    else if(!strcmp(s,"nineteen"))sum+=19;
    else if(!strcmp(s,"twenty"))sum+=20;
    else if(!strcmp(s,"thirty"))sum+=30;
    else if(!strcmp(s,"forty"))sum+=40;
    else if(!strcmp(s,"fifty"))sum+=50;
    else if(!strcmp(s,"sixty"))sum+=60;
    else if(!strcmp(s,"seventy"))sum+=70;
    else if(!strcmp(s,"eighty"))sum+=80;
    else if(!strcmp(s,"ninety"))sum+=90;
    else if(!strcmp(s,"hundred"))
    {
        sum*=100;
    }
    else if(!strcmp(s,"thousand"))
    {
      ans+=sum*1000;
      sum=0;
    }
    else if(!strcmp(s,"million"))
    {
      ans+=sum*1000000;
      sum=0;
    }
    char ch=getchar();
    if(ch=='\n')
    {
      printf("%d\n",ans+sum);
      sum=0;
      ans=0;
    }
  }
  return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值