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
#include
 
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='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<

这题的case比较多。

Tips:

  1. 把9位数(string)分成1-4-4进行处理
  2. 若最高位不为0,每个4位的读法是相同的
  3. 第一个4位若不为0,最后要加上“万”
  4. 注意最中输出的格式,需要擦掉一个space

你可能感兴趣的:(PAT甲级(Advanced Level)练习题——1002)