洛谷 P2108 学英语

题目描述

为了适应紧张的大学学习生活,小Z发愤图强开始复习巩固英语。

由于小Z对数学比较有好感,他首先复习了数词。小Z花了一整天的时间,终于把关于基数词的知识都搞懂了。于是小Z非常兴奋,决定出一些题目考考已经过了英语四级、人称英语帝的小 G。考法很简单:小Z给出某个整数 x 的英文写法,要求小D用阿拉伯数字写出x。

小Z会保证以下几点:

1、-999,999,999 ≤ x ≤ 999,999,999

2、题目中只会用到以下这些英文单词:

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

3、若 x 为负数,题目中第一个单词是 negative,否则任何时候都不会出现 negative 这个词。

4、由于小Z很牛 B,他不知道像 103 这样的数字要写成 one hundred and three 而是直接写成了 one hundred three,就是说小Z的所有题目中都没有写 and 这个词(尽管本应该是要写的),请你谅解。

5、除了第 4 点, 其他还是基本符合英语的语法规则的, 比如 1500 他会写成 one thousand five hundred 而不会写成 fifteen hundred。

小D拿到题目后不屑地说了一句:水题!写个程序么好了……

但是小D要出去玩(此时应该已经在千里之外爽玩了) ,这个任务就交给你了。

输入输出格式

输入格式:

 

一行,题目描述中所说的 x 的英文写法。

 

输出格式:

 

一行, x 的阿拉伯数字写法。

 

输入输出样例

输入样例#1: 复制
six
输出样例#1: 复制
6
输入样例#2: 复制
negative seven hundred twenty nine
输出样例#2: 复制
-729
输入样例#3: 复制
one million one hundred one
输出样例#3: 复制
1000101
输入样例#4: 复制
eight hundred fourteen thousand twenty two
输出样例#4: 复制
814022

说明

【数据规模】

对于100%的数据,-999,999,999 ≤ x ≤ 999,999,999

【时空限制】

0.1s/16M

思路:模拟。

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
map<string,string>ma;
string x;
int tot,maxn;
long long ans;
int num[100000];
void pre(){
    ma["negative"]="-";ma["zero"]="0";ma["one"]="1";ma["two"]="2";ma["three"]="3";
    ma["four"]="4";ma["five"]="5";ma["six"]="6";ma["seven"]="7";ma["eight"]="8";
    ma["nine"]="9";ma["ten"]="10";ma["eleven"]="11";ma["twelve"]="12";ma["thirteen"]="13";
    ma["fourteen"]="14";ma["fifteen"]="15";ma["sixteen"]="16";ma["seventeen"]="17";
    ma["eighteen"]="18";ma["nineteen"]="19";ma["twenty"]="20";ma["thirty"]="30";
    ma["forty"]="40";ma["fifty"]="50";ma["sixty"]="60";ma["seventy"]="70";ma["eighty"]="80";
    ma["ninety"]="90";ma["hundred"]="100";ma["thousand"]="1000";ma["million"]="1000000";
}
int main(){
    pre();
    while(cin>>x){
        x=ma[x];
        if(x[0]=='-'){ cout<<x[0];x.clear();continue; }
        int le=x.length(),sum=0;
        for(int j=0;j<le;j++){
            sum+=x[j]-'0';
            sum*=10;
        }
        sum/=10;
        x.clear();
        num[++tot]=sum;
    }
    for(int i=1;i<=tot;i++){
        if(num[i]<100)    maxn+=num[i];
        else if(num[i]==100)    maxn*=num[i];
        else if(num[i]==1000){
            ans+=maxn*1000;
            maxn=0;
        }
        else if(num[i]==1000000){
            ans+=maxn*1000000;
            maxn=0;
        }
    }
    cout<<ans+maxn;
}

 

posted @ 2017-11-27 20:25  一蓑烟雨任生平  阅读(377)  评论(0编辑  收藏  举报