LuoguP2108 学英语 题解
Content
给出整数 \(x\) 的英文写法,求出这个整数 \(x\)。
规则详见题面。
数据范围:\(|x|\leqslant 999999999\)(\(9\) 个 \(9\))。
Solution
题目思路很简单,但要注意细节。
开两个变量 \(ans\)(最终的答案) 和 \(now\)(当前的计数),边输入一个字符串边一个一个计数,至于对应的操作吗——
如果这个字符串是 \(\texttt{thousand}\) 或者 \(\texttt{million}\) 的话,就直接将 \(now\) 全部计入 \(ans\) 里面,并将 \(now\) 清空。
否则,将读取到的字符串对应的数字加入到 \(now\) 当中(如果是 \(\texttt{hundred}\) 就将 \(now\) 乘上 \(100\))。
注意 \(\texttt{negative}\) 的情况,此时应该在最后输出负数。
具体的思路就是这些。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
string s;
long long num, now, f;
int main() {
f = 1;
while(cin >> s) {
if(s == "negative") f = -1;
if(s == "one") now++;
if(s == "two") now += 2;
if(s == "three") now += 3;
if(s == "four") now += 4;
if(s == "five") now += 5;
if(s == "six") now += 6;
if(s == "seven") now += 7;
if(s == "eight") now += 8;
if(s == "nine") now += 9;
if(s == "ten") now += 10;
if(s == "eleven") now += 11;
if(s == "twelve") now += 12;
if(s == "thirteen") now += 13;
if(s == "fourteen") now += 14;
if(s == "fifteen") now += 15;
if(s == "sixteen") now += 16;
if(s == "seventeen") now += 17;
if(s == "eighteen") now += 18;
if(s == "nineteen") now += 19;
if(s == "twenty") now += 20;
if(s == "thirty") now += 30;
if(s == "forty") now += 40;
if(s == "fifty") now += 50;
if(s == "sixty") now += 60;
if(s == "seventy") now += 70;
if(s == "eighty") now += 80;
if(s == "ninety") now += 90;
if(s == "hundred") now *= 100;
if(s == "thousand") num += now * 1000, now = 0;
if(s == "million") num += now * 1000000, now = 0;
}
num += now;
printf("%lld", num * f);
}
Supplement
建议做完这道题目的读者去做一下【P1617】爱与愁的一千个伤心的理由,大意就是将阿拉伯数字转为英文读法。
顺便推广一下我在这道题的题解(
LuoguP2108 学英语 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- html+css第十篇-命名
命名:根据每块元素的主题 或者功能.在页面上的位置 php 每个单词中间以"_"隔开 #main_left_box{} 驼峰命名 从第二个单词开始每个单词的首字母大写 #mainL ...
- Java设计模式之(十一)——享元模式
1.什么是享元模式? Use sharing to support large numbers of fine-grained objects efficiently. 享元模式(Flyweight ...
- Atcoder Regular Contest 072 C - Alice in linear land(思维题)
Atcoder 题面传送门 & 洛谷题面传送门 首先求出 \(s_i\) 表示经过 \(i\) 次操作后机器人会位于什么位置,显然 \(s_0=D\),\(s_i=\min(s_{i-1},| ...
- DAS,NAS,SAN,简介
根据服务器类型分为:封闭系统的存储和开放系统的存储,封闭系统主要指大型机,开放系统指基于Windows.UNIX.Linux等操作系统的服务器;开放系统的存储分为:内置存储和外挂存储;外挂存储根据连接 ...
- NECAT组装ONT long reads
NECAT 可用于ONT数据的纠错,组装,如果想对ONT long reads进行call SV,也可以使用necatsv. githup网址:https://github.com/xiaochuan ...
- 【Perl示例】整合多个文件
这个需求是在生信分析中几乎天天用到,各种语言都能实现,也都各有特点.这次以perl为例. 已知 文件CT-VS-CON.All.xls为全部蛋白表达矩阵及其差异分析结果. 文件Homo_sapiens ...
- python包之drmaa:集群任务管理
目录 1. drmaa简介 2. 安装和配置 3. 示例 3.1 开始和终止会话 3.2 运行工作 3.3 等待工作 3.4 控制工作 3.5 查询工作状态 4. 应用 4.1 写一个简单应用 4.2 ...
- mysql-加密函数AES_DECRYPT函数
向user表插入数据age字段值为888,并用AES_DECRYPT函数进行加密,key为age(可以自己随意设置,记住就行) insert into user(name,sex,age) value ...
- 深入了解scanf() getchar()和gets()等函数之间的区别
scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些函数除了问题,却找不出其中的原因.下面先看一个很简单的程序: 程序1 ...
- 17.Power of Four-Leetcode
#define IMIN numeric_limits<int>::min() #define IMAX numeric_limits<int>::max() class So ...