PAT_A 1082 Read Number in Chinese

PAT_A 1082 Read Number in Chinese

分析

题目要求将所输入的数字(最多有九位数字位,即输入的整数绝对值小于十亿),转换为中文拼音(读出来)。

看到题目属实没有思路,但由于输入只有九位数字,因此还是可以通过一些不太聪明的方法解决通过(较低质量的AC的代码)。思路为将数字分割为”亿“、”万“、“万以下”三部分分别处理;且第一部分较为简单,后两部分较为类似,侥幸足以通过有限的输入样例。

为了提高水平,找到了一份质量较高的代码来学习(较高质量的AC的代码)。这里也使用了分组的做法,将输入的数字四个作为一组(从低位至高位),分组后再进行进一步的处理。尽管两种做法在思想上具有部分相同的地方,但显然后者(较高质量的AC的代码)的实现方式要高明得多,非常值得学习。

题目的描述

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

较低质量的AC的代码

#include<bits/stdc++.h>

using namespace std;

int main(){
    string num;
    cin>>num;
    if(num=="0"){
        cout<<"ling"<<endl;
        return 0;
    }
    int len = num.length();
    array<string, 10> called ={
      "ling","yi","er","san","si","wu","liu","qi","ba","jiu"  
    };
    array<string,3> ji = {
        "Qian","Bai","Shi"
    };
    if(num[0]=='-'){
        cout<<"Fu ";
        num = num.substr(1);
    }
    string zeros(9-num.length(),'0');
    num = zeros + num;
    bool first = false;
    int c0=0;
    string yi= num.substr(0,1),
        wan = num.substr(1,4),
        ge = num.substr(5);
    if(yi[0]!='0'){
        cout<<called[yi[0]-'0']<<" Yi";
        first = true;
    }
    int fl=0;
    for(int i=0;i<4;i++){
        if(first&&wan[i]=='0'){
            c0++;
        }
        else if(!first&&wan[i]=='0'){
            ;
        }
        else{
            if(i&&c0){
                cout<<" ling";
            }
            {
                fl=1;
                if(!first){
                   first = true;
                }
                else cout<<' ';
                cout<<called[wan[i]-'0'];
                if(i!=3){
                    cout<<" "<<ji[i];
                }
            }
            c0=0;
        }
    }
    if(fl)cout<<" Wan";
    for(int i=0;i<4;i++){
        if(first&&ge[i]=='0'){
            c0++;
        }
        else if(!first&&ge[i]=='0'){
            ;
        }
        else{
            if(i&&c0){
                cout<<" ling";
                c0=0;
            }
            {
                if(!first){
                   first = true;
                }
                else cout<<' ';
                cout<<called[ge[i]-'0'];
                if(i!=3){
                    cout<<" "<<ji[i];
                }
            }
            c0=0;
        }
    }
    cout<<endl;
    return 0;
}

较高质量的AC的代码

#include<cstdio>
#include<cstring>
char num[10][5]={
    "ling","yi","er","san","si","wu","liu","qi","ba","jiu"
};
char wei[5][5]={
    "Shi","Bai","Qian","Wan","Yi"
};
int main(){
    char str[15];
    scanf("%s",str);
    int len = strlen(str);
    int left = 0,right=len-1;
    if(str[0]=='-'){
        printf("Fu");
        left++;
    }
    while(left+4<=right){
        right-=4;
    }
    while(left<len){
        bool flag = false;
        bool isPrint = false;
        while(left <= right){
            if(left > 0 && str[left] == '0'){
                flag = true;
            }
            else{
                if(flag == true){
                    printf(" ling");
                    flag = false;
                }
                if(left > 0){
                    printf(" ");
                }
                printf("%s",num[str[left] - '0']);
                //本节的输出标志
                isPrint = true;
                if(left != right){
                    //在不是个位时输出十,百,千
                    printf(" %s",wei[right - left - 1]);
                }
            }
            left ++;
        }
        if(isPrint == true && right != len-1){
            //在不是个位时输出万或亿
            printf(" %s",wei[(len-1-right)/4 + 2]);
        }
        right += 4;
    }
    return 0;
}
posted @ 2022-01-24 22:57  ghosteq  阅读(27)  评论(0编辑  收藏  举报