Update
- 重新排版了一下此题解,删掉了一些废话。
- 在题解一周年之际修改了 部分中输出打成输入的小错误。
- 改正了 部分中 18 日打成 15 日的一个小错误。
- 感谢题解评论区中@Jiayn 和 @andyli 的指正,已将原有代码进行修改,解决了输入
10输出and ten,输入123输出one hundredtwenty three的问题。
Content
输入一个数 ,输出它的英文读法。
数据范围:。
Solution
其实这道题目蛮简单的,主要是注意一个细节。
Part 1 美式英语中 以内的数的读法
做这个题目让我深感学好英语的重要性。所以,首先我们来讲讲题外话(其实也不算,毕竟这可是本题的核心),了解美式英语中 以内的数的读法,由此得出本题的做法。当然,如果你觉得我很啰嗦的话,也可以直接跳过本章(建议不跳过)。想跟我来的,下面,我们开始吧。
1.1 四位数
四位数,形如的数。关于它的读法有以下几个原则,这里就以为例:(当然,)
-
个十百位都是零,直接读作(每单个数字的读法总晓得吧QAQ)。
-
否则,如果两位是零,有以下三种情况(因为):
(1) 百、十位都是零,读作;
(2) 百、个位都是零,读作;
注意:这里的代表整十位数的读法,例如读作。特别注意!当的时候,前面要加!以下若碰到这种情况,处理方法和这里一样!
(3) 个、十位都是零,读作。
- 否则,如果一位是零,也有三种情况(这个你们都知道的我就不说了QAQ):
(1) 百位是零,前两位读作,后两位的读法有以下两种情况:
一,后两位数并且 ,后两按照11到19的读法读,具体如下:
二,其他情况下,按照(已于上文解释,此处不再重复。)的读法读。
(2)十位是零,读作:。
(3)个位是零,读作:(注意后两位是10的情况!上文已提,此处不提)。
- 那么,其他的数的读法是这样子:。
1.2 三位数
三位数就比四位数简单多了(当然,):
- 当有两位是零时,本来有三种情况的,但那三种情况都会在二位数和一位数中会讲,所以只有以下一种情况,大家稍安勿躁:
十、个位都是零,直接读出。
- 否则,当有一位是零时,有两种情况:
(1)十位是零,读作(注意不能漏)。
(2)个位是零,读作(注意后两位是10的情况)。
- 其他情况下,读作。
1.3 两位数
两位数更简单,直接总起来讲吧。(当然,)
如果个位是零,那么读作(这里就不用考虑 了),否则读作。
1.4 一位数
一位数。。。你只要记着诸如这些单词的拼写就行。
Part 2 正解
由此我们可以发现,上面的这些其实就是这个程序的实现,四位数的最复杂,一位数的最简单,但都要注意,不要犯诸如把等这些单词拼错了,或者特判漏了,还有,这道题目最好要中间调试,以便于看到你整个程序的运行过程(不过交上去的时候记得注释掉)。
Code
#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;
}