跳到正文

题解

P1617的题解

Eason_AC约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月9日
篇幅
约 10 分钟
获赞
13
题目
P1617

版本

  1. v1当前

Update

  • 2020.9.13\texttt{2020.9.13} 重新排版了一下此题解,删掉了一些废话。
  • 2020.9.18\texttt{2020.9.18} 在题解一周年之际修改了 Content\texttt{Content} 部分中输出打成输入的小错误。
  • 2020.9.20\texttt{2020.9.20} 改正了 Update\texttt{Update} 部分中 18 日打成 15 日的一个小错误。
  • 2023.6.26\texttt{2023.6.26} 感谢题解评论区中@Jiayn 和 @andyli 的指正,已将原有代码进行修改,解决了输入 10 输出 and ten ,输入 123 输出 one hundredtwenty three的问题。

Content

输入一个数 nn,输出它的英文读法。

数据范围:n⩽9999n\leqslant 9999。

Solution

其实这道题目蛮简单的,主要是注意一个细节。

Part 1 美式英语中 1000010000 以内的数的读法

做这个题目让我深感学好英语的重要性。所以,首先我们来讲讲题外话(其实也不算,毕竟这可是本题的核心),了解美式英语中 1000010000 以内的数的读法,由此得出本题的做法。当然,如果你觉得我很啰嗦的话,也可以直接跳过本章(建议不跳过)。想跟我来的,下面,我们开始吧。

1.1 四位数

四位数,形如abcd‾\overline{abcd}的数。关于它的读法有以下几个原则,这里就以abcd‾\overline{abcd}为例:(当然,a≠0a≠0)

  1. 个十百位都是零,直接读作a thousand\texttt{a thousand}(每单个数字的读法总晓得吧QAQ)。

  2. 否则,如果两位是零,有以下三种情况(因为C32=3C^2_3=3):

(1) 百、十位都是零,读作a thousand and d\texttt{a thousand and d};

(2) 百、个位都是零,读作a thousand c-ty\texttt{a thousand c-ty};

注意:这里的c-ty\texttt{c-ty}代表整十位数的读法,例如2020读作twenty\texttt{twenty}。特别注意!当c-ty=10\texttt{c-ty=10}的时候,前面要加and\texttt{and}!以下若碰到这种情况,处理方法和这里一样!

(3) 个、十位都是零,读作a thousand b hundred\texttt{a thousand b hundred}。

  1. 否则,如果一位是零,也有三种情况(这个你们都知道的我就不说了QAQ):

(1) 百位是零,前两位读作a thousand\texttt{a thousand},后两位的读法有以下两种情况:

一,后两位数⩾11\geqslant11并且⩽19\leqslant19 ,后两按照11到19的读法读,具体如下:

11 eleven11~eleven
12 twelve12~twelve
13 thirteen13~thirteen
14 fourteen14~fourteen
15 fifteen15~fifteen
16 sixteen16~sixteen
17 seventeen17~seventeen
18 eighteen18~eighteen
19 nineteen19~nineteen

二,其他情况下,按照c-ty d\texttt{c-ty d}(c-ty\texttt{c-ty}已于上文解释,此处不再重复。)的读法读。

(2)十位是零,读作:a thousand b hundred and d\texttt{a thousand b hundred and d}。

(3)个位是零,读作:a thousand b hundred c-ty\texttt{a thousand b hundred c-ty}(注意后两位是10的情况!上文已提,此处不提)。

  1. 那么,其他的数的读法是这样子:a thousand b hundred c-ty d\texttt{a thousand b hundred c-ty d}。

1.2 三位数

三位数就比四位数简单多了(当然,a=0,b≠0a=0,b≠0):

  1. 当有两位是零时,本来有三种情况的,但那三种情况都会在二位数和一位数中会讲,所以只有以下一种情况,大家稍安勿躁:

十、个位都是零,直接读出b hundred\texttt{b hundred}。

  1. 否则,当有一位是零时,有两种情况:

(1)十位是零,读作b hundred and d\texttt{b hundred and d}(注意and\texttt{and}不能漏)。

(2)个位是零,读作b hundred (and) c-ty\texttt{b hundred (and) c-ty}(注意后两位是10的情况)。

  1. 其他情况下,读作b hundred c-ty d\texttt{b hundred c-ty d}。

1.3 两位数

两位数更简单,直接总起来讲吧。(当然,a=0,b=0,c≠0a=0,b=0,c≠0)

如果个位是零,那么读作c-ty\texttt{c-ty}(这里就不用考虑 1010 了),否则读作c-ty d\texttt{c-ty d}。

1.4 一位数

一位数。。。你只要记着诸如one,two,three,etc.\texttt{one,two,three,etc.}这些单词的拼写就行。

Part 2 正解

由此我们可以发现,上面的这些其实就是这个程序的实现,四位数的最复杂,一位数的最简单,但都要注意,不要犯诸如把one,two,three,etc.\texttt{one,two,three,etc.}等这些单词拼错了,或者特判漏了,还有,这道题目最好要中间调试,以便于看到你整个程序的运行过程(不过交上去的时候记得注释掉)。

Code

C++
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;

const string num[11] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
const string ty[11] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
const string teen[21] = {"", "", "", "", "", "", "", "", "", "", "", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
int a[5], n, cnt;

int main() {
	scanf("%d", &n);
	int tmp = n;
	while(tmp) {
		a[++cnt] = tmp % 10;
		tmp /= 10;
	}
	int teens = a[2] * 10 + a[1];
//	for(int i = cnt; i >= 1; --i)	printf("%d ", a[i]);
//	puts("");
	if(cnt == 4) {
		cout << num[a[4]] << ' ';printf("thousand");
		if(a[1] == 0 && a[2] == 0 && a[3] == 0)
			return 0;
		else if(a[3] == 0 && a[2] && a[1]) {
			putchar(' ');
			if(teens >= 11 && teens <= 19) {
				cout << teen[teens];
				return 0;
			}
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << "and " << ty[a[2]];
			putchar(' ');
			cout << num[a[1]];
		} else if(a[3] && !a[2] && a[1]){
			putchar(' ');
			cout << num[a[3]];
			printf(" hundred and ");
			cout << num[a[1]];
		} else if(a[3] && a[2] && !a[1]) {
			putchar(' ');
			cout << num[a[3]];
			printf(" hundred ");
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << ty[a[2]];
		} else if(a[3] && !a[2] && !a[1]) {
			cout << ' ' << num[a[3]];
			printf(" hundred");
		}
		else if(!a[3] && a[2] && !a[1]) {
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << ty[a[2]];
		}
		else if(!a[3] && !a[2] && a[1])
			cout << " and " << num[a[1]];
		else {
			cout << ' ' << num[a[3]] << " hundred ";
			if(teens >= 11 && teens <= 19)
				cout << teen[teens];
			else {
				cout << ty[a[2]] << ' ' << num[a[1]];
			}
		}  
	} else if(cnt == 3) {
		cout << num[a[3]]; printf(" hundred");
		if(!a[2] && !a[1])	return 0;
		else if(a[2] && !a[1]) {
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << ty[a[2]];
		}
		else if(!a[2] && a[1]) {
			printf(" and ");
			cout << num[a[1]];
		} else if(teens < 11 || teens > 19) {
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << ty[a[2]];
			cout << ' ' << num[a[1]];
		} else
			cout << ' ' << teen[teens];
	} else if(cnt == 2) {
		if(teens >= 11 && teens <= 19)
			cout << teen[teens];
		else if(!a[1]) {
			if(a[2] == 1)	cout << " and " << ty[1];
			else	cout << ty[a[2]];
		}
		else
			cout << ty[a[2]] << ' ' << num[a[1]];
	} else
		cout << num[a[1]];
	return 0;
}
原文
存档评论13 条抓取于