【PAT】A-1082 Read Number in Chinese【字符串处理】

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

题目大意

输出一个数字的拼音读音。

思路

将一个数字看做一个二维的结构,从低位开始每四个一组,大单位分别是"个(实际不说出来)"、“万”、“亿”,每组内的数字又有小单位“千”、“百”、“十”、“个(实际不说出来)”。
这道题主要是对于零的处理比较复杂,最大的问题是我真的不知道要怎么念啊~。

代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <iostream>
using namespace std;
char smallUnit[][5] = {"Shi", "Bai", "Qian"};
char bigUnit[][5] = {"Wan", "Yi"};
char num[][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main() {
    string str;
    queue<string> result;
    cin >> str;
    
    // 处理负号
    if(str[0] == '-'){
        result.push("Fu");
        str.erase(0,1);
    }
    
    // 处理前导零
    while (str.length() > 0 && str[0] == '0') {
        str.erase(0,1);
    }
    
    // 输出全是零的情况
    if(str.length() == 0){
        cout << "ling";
        return 0;
    }
    
    
    int len = (int)str.length();
    // 每四位一组,输出组数
    for(int group = len / 4; group >= 0; group--){
        // 两个标记 有没有零和是否输出了数字
        bool zero = false, hasPrint = false;
        char n;
        int t;
        for(int j = max(0, len - (group * 4 + 3) - 1), l = len - group * 4; j < l; j++){
            n = str[j] - '0';
            if(n == 0){
                zero = true;
            }else{
                hasPrint = true;
                if(zero){
                    result.push("ling");
                    zero = false;
                }
                result.push(num[n]);
                t = l - j - 2;
                if(t >= 0){
                    result.push(smallUnit[t]);
                }
            }
        }
        // 如果该组不是第一组(第一组没有单位)且该组中输出了数字(比如100001000中第二组的“万”不会被输出)
        if(group > 0 && hasPrint){
            result.push(bigUnit[group - 1]);

        }
    }
    // 输出第一个拼音(没有前导空格)
    if(!result.empty()){
        cout << result.front();
        result.pop();
    }
    
    // 输出后面的拼音(带前导空格)
    while(!result.empty()){
        cout << " " <<result.front();
        result.pop();
    }
    
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值