POJ 2121 Inglish-Number Translator

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

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;
}

POJ 2121 Inglish-Number Translator,布布扣,bubuko.com

时间: 2024-08-02 08:41:15

POJ 2121 Inglish-Number Translator的相关文章

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP

【POJ 1019】 Number Sequence

[POJ 1019] Number Sequence 二分水题 放组合数学里...可能有什么正规姿势吧Orz 112123123412345...这种串 分成长度1 2 3 4 5...的串 注意有多位数 把长度累加到一个数组里 注意要累加 因为查询的时候查的是原串中对应位置的数 因此要累加上前一次的长度 然后二分处该串前的总长 用查询的位置-之前串的总长 就是在最长的串中的位置 因此还要打个最长串的表 这些我都写一个循环里了 看着有点乱 可以拆开写... 代码如下: #include <ios

POJ 2329 -- Nearest number - 2

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4224   Accepted: 1308 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your program must repla

[划分树] POJ 2104 K-th Number

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

字符串处理,Poj(2121)

题目链接:http://poj.org/problem?id=2121 差一点就WA哭了,主要是自己傻逼了. 思路: 遇到hundred,sum*100; 但是遇到thouthend,million,ans+=sum*(... ...),sum=0; 因为到了thouthend,million,后面肯定又是新的数字,跟前面的没关系了,sum=0; 而hundred,后面是可以再加数的,比如22,所以这里只要sum*=100; 注意: 这里经典的一招,杰哥教我的,要重置一下order,只需要ord

POJ 2014.K-th Number 区间第k大 (归并树)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

POJ 2121

http://poj.org/problem?id=2121 一道字符串的转换的题目. 题意:就是把那个英文数字翻译成中文. 思路:首先打表,然后把每一个单独的单词分离出来,在组合相加相乘. 1 #include <stdio.h> 2 #include <string.h> 3 4 struct trans{ 5 char eng[10]; 6 int num; 7 }s[40]; 8 9 int main(){ 10 // freopen("in.txt",

POJ 2104 K-th Number(分块+二分)

题目链接:http://poj.org/problem?id=2104 题目: Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return qu

POJ 2104 K-th Number (划分树)

                                                            K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 52651   Accepted: 18091 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures