PAT甲级刷题记录——1082 Read Number in Chinese (25分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fufirst 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

思路

这题怎么说呢,看上去简单,但是处理的东西很杂很乱也很多。

首先,题目描述得也不清楚,何谓“Chinese tradition”,根本没有把每个情况告诉我们,只能靠我们自己去想。我这里总结一下,大概是这样的:把这个输入的数四位四位一切,如果每一节(四位数字)的低位存在连续的“0”,那么就不输出“ling”,如果出现x00x这种情况,那么只输出一个“0”。

但是我这个代码其实是不完善的,我只判了x00x这种中间连续“0”的情况以及最低位连续“0”的情况,居然也AC了(说明测试点并没有刁难我们……想要完全正确的可以看看《上机训练实战指南》里晴神给出的测试样例)……事实上,应该不止判断最低位连续“0”的情况,每一节都需要判断它的低位是否存在连续“0”,比如:x000x000,这样的话,中间的那三个“0”以及最后的三个“0”应该是一个都不输出的(我的代码会把中间的那串“0”当做是x00x的情况输出一个“ling”),但是这题实在是有点折磨人,AC了我就没改了(逃)。

  • 为了处理低位是否是连续的“0”,我这里用了zeroLen来计算低位0的个数,当i的值大于zeroLen的时候,才说明可以输出“ling”;
  • 为了处理中间连续“0”的情况(即:x00x的情况),我用了一个bool类型的flag来标记(初始化为false),如果当前位是“0”,且flag是false,那么输出“ling”,并且置flag为true,这样的话,就算碰到下一个还是“0”,因此flag已经是true了,也不会输出。这里要注意的是,如果当前位不是“0”,那么要记得把flag回归到false状态,否则整个数只能输出一次“ling”。

附:晴神给出的测试样例

0			//ling
8			//ba
808080808	//ba Yi ling ba Bai ling ba Wan ling ba Bai ling ba
-880808080	//Fu ba Yi ba Qian ling ba Shi Wan ba Qian ling ba Shi
800000008	//ba Yi ling ba
800000000	//ba Yi
80000008	//ba Qian Wan ling ba
80008000	//ba Qian Wan ba Qian
80000000	//ba Qian Wan

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
string name[10] = {"gnil", "iy", "re", "nas", "is", "uw", "uil", "iq", "ab", "uij"};
int main()
{
    string n, result;
    cin>>n;
    if(n[0]=='0'&&n.length()==1) cout<<"ling";
    else{
        bool isPositive = true;
        if(n[0]=='-'){
            isPositive = false;
            n.erase(n.begin());//删负号
        }
        reverse(n.begin(), n.end());//低位在前
        int zeroLen = 0;//记录尾部0的个数
        for(int i=0;i<n.length();i++){
            if(n[i]!='0') break;
            zeroLen ++;
        }
        bool flag = false;
        for(int i=0;i<n.length();i++){
            if(i%4==0){
                if(i/4==1){
                    result += "naW ";
                }
                else if(i/4==2){
                    result += "iY ";
                }
            }
            else if(i%4==1&&n[i]!='0'){
                result += "ihS ";
            }
            else if(i%4==2&&n[i]!='0'){
                result += "iaB ";
            }
            else if(i%4==3&&n[i]!='0'){
                result += "naiQ ";
            }
            if(n[i]!='0'){
                flag = false;
                result += name[n[i]-'0'];
                result += ' ';
            }
            else if(n[i]=='0'&&i>zeroLen&&flag==false){
                flag = true;//防止出现2个及以上的0输出多个"ling"
                result += name[n[i]-'0'];
                result += ' ';
            }
        }
        reverse(result.begin(), result.end());
        if(isPositive==false) result.insert(0, "Fu");
        if(result[0]==' ') result.erase(result.begin());//删去头部的空格
        if(result[result.length()-1]==' ') result.erase(result.begin()+result.length()-1);//删去尾部的空格
        cout<<result;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值