P1617的题解

· · 题解

Update

Content

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

数据范围:n\leqslant 9999

Solution

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

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

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

1.1 四位数

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

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

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

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

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

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

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

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

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

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

11~eleven 12~twelve 13~thirteen 14~fourteen 15~fifteen 16~sixteen 17~seventeen 18~eighteen 19~nineteen

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

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

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

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

1.2 三位数

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

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

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

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

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

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

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

1.3 两位数

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

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

1.4 一位数

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

Part 2 正解

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

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;
}