ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)
Give Me the Number
Time Limit: 2 Seconds Memory Limit: 65536 KB
Numbers in English are written down in the following way (only numbers less than 109 are considered). Number abc,def,ghi is written as "[abc] million [def] thousand [ghi]". Here "[xyz]" means the written down number xyz .
In the written down number the part "[abc] million" is omitted if abc = 0 , "[def] thousand" is omitted if def = 0 , and "[ghi] " is omitted if ghi = 0 . If the whole number is equal to 0 it is written down as "zero". Note that words "million" and "thousand" are singular even if the number of millions or thousands respectively is greater than one.
Numbers under one thousand are written down in the following way. The number xyz is written as "[x] hundred and [yz] ”. ( If yz = 0 it should be only “[x] hundred”. Otherwise if y = 0 it should be only “[x] hundred and [z]”.) Here "[x] hundred and" is omitted if x = 0 . Note that "hundred" is also always singular.
Numbers under 20 are written down as "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", and "nineteen" respectively. Numbers from 20 to 99 are written down in the following way. Number xy is written as "[x0] [y] ", and numbers divisible by ten are written as "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", and "ninety" respectively.
For example, number 987,654,312 is written down as "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve", number 100,000,037 as "one hundred million thirty seven", number 1,000 as "one thousand". Note that "one" is never omitted for millions, thousands and hundreds.
Give you the written down words of a number, please give out the original number.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1900) which is the number of test cases. It will be followed by T consecutive test cases.
Each test case contains only one line consisting of a sequence of English words representing a number.
Output
For each line of the English words output the corresponding integer in a single line. You can assume that the integer is smaller than 109.
Sample Input
3
one
eleven
one hundred and two
Sample Output
1
11
102
Author: CAO, Peng
Source: The 5th Zhejiang Provincial Collegiate Programming Contest
#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<string,int> mp;
char ch[],str[];
int t,ans,num;
int main()
{ mp.clear();
mp["zero"]=; mp["one"]=;
mp["two"]=; mp["three"]=;mp["four"]=;
mp["five"]=;mp["six"]=;mp["seven"]=; mp["eight"]=;
mp["nine"]=;mp["ten"]=; mp["eleven"]=;
mp["twelve"]=;mp["thirteen"]=; mp["fourteen"]=;
mp["fifteen"]=; mp["sixteen"]=; mp["seventeen"]=;
mp["eighteen"]=;mp["nineteen"]=;
mp["twenty"]=; mp["thirty"]=; mp["forty"]=;
mp["fifty"]=; mp["sixty"]=; mp["seventy"]=;
mp["eighty"]=; mp["ninety"]=; while(~scanf("%d",&t))
{
getchar();
for(;t>;t--)
{
gets(ch);
//getchar();
int len=strlen(ch),l=-;
ch[len]=' '; len++;
memset(str,,);
ans=; num=;
for(int i=;i<len;i++)
{
if (ch[i]!=' ') str[++l]=ch[i];
else
{
if (mp.find(str)!=mp.end()) num+=mp[str];
else
{
if (strcmp(str,"million")==)
{num=num*; ans+=num; num=;}
if (strcmp(str,"thousand")==)
{num=num*; ans+=num; num=;}
if (strcmp(str,"hundred")==)
num=num*;
}
//str[]='';
memset(str,,);//把字符串数组清空的神奇方法
l=-;
} }
printf("%d\n",ans+num);
}
}
return ;
}
ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)的更多相关文章
- ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...
- ZOJ 2971 Give Me the Number
Give Me the Number Numbers in English are written down in the following way (only numbers less than ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- C语言博客作业—字符数组
一.PTA实验作业 题目1:字符串转换成十进制整数 1. 本题PTA提交列表 2. 设计思路 (1)定义i为循环变量,number用于存放每一次转化的结果,flag用于判断是否为负数,p用于修改结果的 ...
- C语言---字符数组
一.PTA实验作业 题目1:7-2 统计一行文本的单词个数 1. 本题PTA提交列表 2. 设计思路 定义循环变量i,j定义不为空格的字符数count,定义单词数number,i,j,count,nu ...
- Java-IO之CharArrayWriter(字符数组输出流)
CharArrayWriter用于写数据,数据单位是字符. (1) 通过CharArrayWriter()创建的CharArrayWriter对应的字符数组大小是32. (2) 通过CharArray ...
- Codeforces Round #369 (Div. 2) A. Bus to Udayland【字符串/二维字符数组求连起来的座位并改为其他字符】
A. Bus to Udayland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- C语言中的字符数组和字符串
在C语言中,没有字符串类型,但是可以通过字符数组来模拟字符串. 字符串可以在栈上,堆上,静态区和常量区进行分配. char buf[50] = "abc"; char buf[] ...
- Java 学习(6):java Number & Math & String & 数组...常用类型
目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...
随机推荐
- TOSCA自动化测试工具ppt(正在整理)
1. 认识TOSCA 安装使用 2. TOSCA自动化测试工具的优点 1). 可扩展 Tosca Commander™ functionalities can be extended by us ...
- SpringBoot Web项目中中如何使用Junit
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- C++ 第三十三天
Ⅰ.类成员函数的隐式参数 T *const this . 就是说对于某个类的成员函数 returnType function() 的真实面目其实是这样的 returnType function(T * ...
- 【Thinking in Java, 4e】初始化与清理
[用构造器确保初始化] [方法重载] 涉及基本类型的重载 1.如果传入的数据类型小于方法中声明的形式参数类型:实际数据类型会被提升. 2.如果传入的数据类型大于方法中声明的形式参数类型:编译器报错,应 ...
- Windos 系统网络连接 调优
1.运行进入注册表:regedit 2.进入注册表指定路径 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters ...
- 20145310《Java程序设计》第3次实验报告
20145310<Java程序设计>第3次实验报告 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 实验步骤 首先使用git设置用户名和ssh公钥 使用git ...
- SVN一直提示需要clean up
无论到那一级都提示clean up, 这是陷入clean up 死循环的结果. 解决办法: 使用任何一款可以连sqllit 的数据库管理软件例如(Navicat Premium),连入 项目跟目录/. ...
- params
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch06 ...
- C++ 单词接龙
问题描述: 拉姆刚刚开始学习英文字母,对单词排序很感兴趣,他能够迅速确定是否可以将这些单词排列在一个列表中,使得该列表中任何单词的首字母与前一个单词的尾字母相同,力能编写一个计算机程序帮助拉姆进行判断 ...
- mac下搭建基于vue-cli 3.0的Element UI 项目
1.安装yarn管理工具(包含node.js); 2.安装全局vue-cli全家桶: yarn global add @vue/cli 3.创建.测试一个vue-cli项目: vue create a ...