POJ 2121 Inglish-Number Translator


Inglish-Number Translator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4475 Accepted: 1747

Description

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer represen
ation. 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

CTU Open 2004,UVA 486

题意: 将英文写的数字转化为阿拉伯数字~~

~~~第一次用map做题..........
这题只要方法找到了,编码实现很简单~~而我就是在找方法这里花了不少时间。

#include<iostream>
#include<string> 
#include<map>
using namespace std;
int main()
{
//建立英文数字对应关系 
map<string ,long long > map1;
map1["one"]=1; 
map1["negative"]=-1;
map1["two"]=2;
map1["three"]=3;
map1["four"]=4;
map1["five"]=5; 
map1["six"]=6;
map1["seven"]=7;
map1["eight"]=8;
map1["nine"]=9;
map1["ten"]=10;
map1["eleven"]=11;
map1["twelve"]=12;
map1["thirteen"]=13;
map1["fourteen"]=14;
map1["fifteen"]=15;
map1["sixteen"]=16;
map1["seventeen"]=17;
map1["eighteen"]=18;
map1["nineteen"]=19;
map1["twenty"]=20;
map1["thirty"]=30;
map1["forty"]=40;
map1["fifty"]=50;
map1["sixty"]=60;
map1["seventy"]=70;
map1["eighty"]=80;
map1["ninety"]=90;
map1["hundred"]=100;
map1["thousand"]=1000;
map1["million"]=1000000;
long long temp,now,r;
string str,num;
while(getline(cin,str))
{
  	if(str=="")     //输入为空则终止 
  	break;  
	//sign 表示符号,r存放结果,now为当前值   
  	long long sign=1,temp=0,r=0,now=0;
	//start和substr配合用于截取数字 
    long long pos=0,start=0;
  	long long L=str.size();
  	for(long long i=start;i<=L;i++)
  	{
  		if(str[i]==' '||i==L)
  		{
  			num=str.substr(start,i-start);
  			now=map1[num];
  			if(now==-1)
  			sign=now;
  			else 
			{
  			if(now>0&&now<100)
  			 temp+=now;
  			else
  			{
			   if(now==100)
			   temp*=100;
			   else 
			   {
			   	r+=temp*now;
			   	temp=0;
			   } 
  			}	
  			}
			start=i+1;
  		}
  	}
  	r+=temp;
  	cout<<sign*r<<endl;
}
  return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值