P1617的题解
Update
Content
输入一个数
数据范围:
Solution
其实这道题目蛮简单的,主要是注意一个细节。
Part 1 美式英语中 10000 以内的数的读法
做这个题目让我深感学好英语的重要性。所以,首先我们来讲讲题外话(其实也不算,毕竟这可是本题的核心),了解美式英语中
1.1 四位数
四位数,形如
-
个十百位都是零,直接读作
\texttt{a thousand} (每单个数字的读法总晓得吧QAQ)。 -
否则,如果两位是零,有以下三种情况(因为
C^2_3=3 ):
(1) 百、十位都是零,读作
(2) 百、个位都是零,读作
注意:这里的
(3) 个、十位都是零,读作
- 否则,如果一位是零,也有三种情况(这个你们都知道的我就不说了QAQ):
(1) 百位是零,前两位读作
一,后两位数
二,其他情况下,按照
(2)十位是零,读作:
(3)个位是零,读作:
- 那么,其他的数的读法是这样子:
\texttt{a thousand b hundred c-ty d} 。
1.2 三位数
三位数就比四位数简单多了(当然,
- 当有两位是零时,本来有三种情况的,但那三种情况都会在二位数和一位数中会讲,所以只有以下一种情况,大家稍安勿躁:
十、个位都是零,直接读出
- 否则,当有一位是零时,有两种情况:
(1)十位是零,读作
(2)个位是零,读作
- 其他情况下,读作
\texttt{b hundred c-ty d} 。
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;
}