PAT甲级(Advanced Level)练习题——1002

前几天去东莞比赛,赶上台风。
http://p.weather.com.cn/2017/08/2763670.shtml#p=1
晚上坐飞机回学校,穿过云层之后看到电闪雷鸣,大概是受影响产生的恶劣天气。
广东的同学又要躲在寝室里瑟瑟发抖了。。

题目描述
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".

输入描述:
Each input file contains one test case, which gives an integer with no more than 9 digits.

输出描述:
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.

输入例子:
-123456789

输出例子:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

代码:

#include<iostream>
#include<string>
 
using namespace std;
 
string number[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
 
string wei[4]={"Qian","Bai","Shi",""};
 
int main()
{
    string re;
    int flag = 0,i;
    string a;
    cin>>a;
    if(a[0] == '-')
    {
        re += "Fu ";
        a.erase(0,1);
    }
    if(a.size() == 9)
    {
        re += number[ (int)(a[0]-'0') ];
        re += " Yi ";
        a.erase(0,1);
    }
    int len = a.size();
    int m;//记录位数
    m = (len % 4); //初始化位数
    int lingc = 0;//记录0的次数
    if(m % 2 == 1)
    {
        m = 4 -m;
    }
    for(i=0;i<len;i++)
    {
        if(a[i] =='0' && len == 1)
        {
            re += "ling ";
        }
        else if((int)(a[i]-'0') != 0)
        {
            if(lingc == 4)
            {
                re += "ling ";
                lingc = 0;
            }
            re += number[ (int)(a[i]-'0') ];
            re += " ";
            re += wei[m++];
            if(m!=4)
                re += " ";
        }
        else if( a[i+1]>='1' && a[i+1]<='9' && m<3)
        {
            re += "ling ";
            m++;
        }
        else
        {
            m++;
            lingc++;
        }
 
 
        if(m == 4 && len>4 && i<4)
        {
            if(lingc !=4)
                re += "Wan ";
            m = 0;
        }
    }
    len = re.size();
    re.erase(len-1,1);
    cout<<re<<endl;
    return 0;
}

这题的case比较多。

Tips:

  1. 把9位数(string)分成1-4-4进行处理
  2. 若最高位不为0,每个4位的读法是相同的
  3. 第一个4位若不为0,最后要加上“万”
  4. 注意最中输出的格式,需要擦掉一个space
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 都是网上找的资料,如有侵权,请联系我。 No.1001 Rational Sum(20) 1.题目描述 Given...
    JaiUnChat阅读 5,101评论 0 0
  • 如果雪是温暖的,寒冷是何物? 如果爱是永恒的,变化是何故? 如果世界有尽头,始终是何处? 如果极乐有归舟,彼岸将何渡?
    时溢阅读 1,898评论 1 1
  • 大家好,我是卷,福建人。我是一名职业女性,二宝妈,同时还是你们的异类小伙伴。来自人生效率手册16班以及演讲铁...
    卷Jane阅读 1,651评论 3 2
  • 从第一集新选组英姿飒爽地完成任务后,时间轴就回到多年前新选组还未正式成立的时候。 上集讲到胜和岁与桂结下梁子,又因...
    drinkingwater阅读 2,586评论 0 0