[洛谷]P2108 学英语 (#模拟 -2.3)

题目描述

为了适应紧张的大学学习生活,小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

思路

看似很高端的题目,其实也就模拟搞定。

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
string str;
long long int s,a;
void work()
{
	if (str=="one")
		s++;
	else if (str=="two")
		s=s+2;
	else if (str=="three")
		s=s+3;
	else if (str=="four")
		s=s+4;
	else if (str=="five")
		s=s+5;
	else if (str=="six")
		s=s+6;
	else if (str=="seven")
		s=s+7;
	else if (str=="eight")
		s=s+8;
	else if (str=="nine")
		s=s+9;
	else if (str=="ten")
		s=s+10;
	else if (str=="eleven")
		s=s+11;
	else if (str=="twelve")
		s=s+12;
	else if (str=="thirteen")
		s=s+13;
	else if (str=="fourteen")
		s=s+14;
	else if (str=="fifteen")
		s=s+15;
	else if (str=="sixteen")
		s=s+16;
	else if (str=="seventeen")
		s=s+17;
	else if (str=="eighteen")
		s=s+18;
	else if (str=="nineteen")
		s=s+19;
	else if (str=="twenty")
		s=s+20;
	else if (str=="thirty")
		s=s+30;
	else if (str=="forty")
		s=s+40;
	else if (str=="fifty")
		s=s+50;
	else if (str=="sixty")
		s=s+60;
	else if (str=="seventy")
		s=s+70;
	else if (str=="eighty")
		s=s+80;
	else if (str=="ninety")
		s=s+90;
	else if (str=="hundred")//乘100即可 
		s=s*100;
	else if (str=="thousand")
	{//千与百万前可能出现one hundred thirty two的情况,所以除了thousand与million两词外的词,都不能直接加到结果中。
		a=a+s*1000;
		s=0;//只要碰到千和百万就可以清0了 
	}
	else if (str=="million")
	{
		a=a+s*1000000;
		s=0;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	int flag(0);
	while (cin>>str)
	{
		if(str=="negative")//负数 
			flag=1;
		work();
		str="";//字符串清空 
	}
	work();
	a=a+s;//还有剩下的一次 
	if(flag)
	{
		a=-a;
	}
	cout<<a<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Apro1066/article/details/81266989
2.3