PAT甲级1082 Read Number in Chinese

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
结尾无空行

折磨点:
1、正负数、格式错误:首先正负数会导致我在下面设定的下标转换出现问题,因为我的下标转换规定字符串必须从1开始,因此需要在正数字符串最前面插入一个字符,并记录字符串长度和字符串中纯数字的位数。
其次就是这个格式错误,这个搞了我很久(因为比较少碰到,要错也是答案错误。。。),不断测试后才发现测试结果第一个数字前面有一个小空格(近视眼真没看出来),其实就是因为我在输出时默认结果前面带一个空格,当正数时就会格式错误多一个空格

2、数字中间有0:详情请看用例2,做题时又没注意,以为他会像个正常人一样读shi wan。。。以上是0出现在万位的情况下,flag需要重置,因为万位的0不需要后面补上输出。另外就是需要读ling的情况,这个就是通过设置flag实现

3、只有1位数字而且是0的情况下,这是其中一个用例,需要特殊处理

#include <iostream>
#include <string>
using namespace std;
string num[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string pos[] = {" ","Shi","Bai","Qian"};
string big[] = {" ","Wan","Yi"};
int main(){
    string s,str;
    str = "#";
    bool isfirst = true;
    cin >> s;
    if(s == "0"||s == "-0"){
        cout << "ling";
        return 0;
    }
    if(s[0] == '-'){
        cout << "Fu";
        s.erase(s.begin());
        isfirst = false;
    }
    str += s;
    int len = str.size();
    int start = 1;
    int num_len = len - 1;
    int flag = 0;
    for(int i = start;i < len;++i){
        if(str[i] == '0'){
            flag = 1;
            if(num_len - i == 4){
                cout << ' ' << "Wan";
                flag = 0;
            }
        }
        else{
            if(flag == 1){
                cout << ' ' << "ling";
                flag = 0;
            }
            if(isfirst == true){
                cout << num[str[i] - '0'];
                isfirst = false;
            }
            else{
                cout << ' ' << num[str[i] - '0'];
            }
            if((num_len - i) % 4 != 0){
                cout << ' ' << pos[(num_len - i) % 4];
            }
            if((num_len - i) / 4 != (num_len - i - 1) / 4&&num_len - i - 1 > 0){
                cout << ' ' << big[(num_len - i) / 4];
            }
        }
    }
    cout << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值