首页 > 代码库 > POJ 2121 Inglish-Number Translator
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
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:
The input is terminated by an empty line.
- Negative numbers will be preceded by the word negative.
- 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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。