poj2121
2016-07-23 17:14
405 查看
Inglish-Number Translator
Description
In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. 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:
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
Sample Output
最有杀伤力的一个one million one hundred eleven thousand one hundred eleven
这里要注意的就是
主要就是叠加的问题
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5166 | Accepted: 2030 |
In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. 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:
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
最有杀伤力的一个one million one hundred eleven thousand one hundred eleven
这里要注意的就是
<span style="white-space:pre"> </span>if(input==a[i].str){ if(i==30){ ans+=temp*a[i].num; temp=0; } else if(i==29){ ans+=temp*1000; temp=0; } else if(i==28) temp*=100; else temp+=a[i].num; break; }
主要就是叠加的问题
#include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<algorithm> using namespace std; struct number { string str; int num; }a[40]; void deal() { a[0].num=0;a[0].str="zero"; a[1].num=1;a[1].str="one"; a[2].num=2;a[2].str="two"; a[3].num=3;a[3].str="three"; a[4].num=4;a[4].str="four"; a[5].num=5;a[5].str="five"; a[6].num=6;a[6].str="six"; a[7].num=7;a[7].str="seven"; a[8].num=8;a[8].str="eight"; a[9].num=9;a[9].str="nine"; a[10].num=10;a[10].str="ten"; a[11].num=11;a[11].str="eleven"; a[12].num=12;a[12].str="twelve"; a[13].num=13;a[13].str="thirteen"; a[14].num=14;a[14].str="fourteen"; a[15].num=15;a[15].str="fifteen"; a[16].num=16;a[16].str="sixteen"; a[17].num=17;a[17].str="seventeen"; a[18].num=18;a[18].str="eighteen"; a[19].num=19;a[19].str="nineteen"; a[20].num=20;a[20].str="twenty"; a[21].num=30;a[21].str="thirty"; a[22].num=40;a[22].str="forty"; a[23].num=50;a[23].str="fifty"; a[24].num=60;a[24].str="sixty"; a[25].num=70;a[25].str="seventy"; a[26].num=80;a[26].str="eighty"; a[27].num=90;a[27].str="ninety"; a[28].num=100;a[28].str="hundred"; a[29].num=1000;a[29].str="thousand"; a[30].num=1000000;a[30].str="million"; a[31].num=-1;a[31].str="negative"; } int main() { deal(); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); string input; char ch; int ans=0,temp=0; while(cin>>input) { if(input==a[31].str) cout<<'-'; for(int i=0;i<31;i++){ if(input==a[i].str){ if(i==30){ ans+=temp*a[i].num; temp=0; } else if(i==29){ ans+=temp*1000; temp=0; } else if(i==28) temp*=100; else temp+=a[i].num; break; } } ch=getchar(); if(ch=='\n'){ cout<<ans+temp<<endl; ans=0; temp=0; } } return 0; }
相关文章推荐
- 算法 - 树状结构
- hdu 3666 THE MATRIX PROBLEM(差分约束,思路)
- Python从入门到放弃(四):函数细讲
- 类的拷贝构造函数、赋值函数
- android stutio 使用viewpager创建一个滑动图片浏览app
- #科委外文文献发现系统——导出word模板1.0
- POJ 1328 Radar Installation(雷达布防)
- LeetCode#20 Valid Parentheses
- Struts1.3——Struts标签
- HDU 5732 Subway
- 奶牛的锻炼-线性dp
- 个人android开发风险得与失
- Linux终端常用快捷键
- php项目笔记(二)php部分
- C#基础之枚举
- mongodb update
- OOC 面向对象C语言编程实践
- 重学搜索yi.2:Lake Counting--dfs
- 浅谈OOP与AOP关系
- 我刚刚写了一个map的题目,还用到了#include<string>的头文件,下面我来总结一下 map<类型>变量 q q.find(找字符串) q.end()是否到达末尾