PAT1082:Read Number in Chinese

Posted 0kk470

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT1082:Read Number in Chinese相关的知识,希望对你有一定的参考价值。

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.将该数的每一位数字用一个int数组的形式存放。
3.遍历该数组,根据该数组的每一位的位数和单个数字将对应的字符串插入到一个新的vector中,为0的位数除了是万位或者亿位以外都不用插入位数的字符串。
4.对于多余的零做处理,并输出每一位的数字和位数。

代码
#include<iostream>
#include<vector>
using namespace std;
vector<string> n={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
vector<string> digit={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};

using namespace std;
int main()
{
   vector<string> res;
   vector<int> number;
   int num,index;
   cin >> num;
   if(num == 0)
   {
      cout << "ling" << endl;
      return 0;
   }
   else if( num < 0)
   {
       cout << "Fu ";
       num = -num;
   }
   while(num != 0)
   {
       number.push_back(num % 10);
       num /= 10;
   }

   for(index = 0;index < number.size() && number[index] == 0;index++);
   if(index == 8)
   {
       cout << n[number[index]] << " Yi";
       return 0;
   }
   for(int i = index;i < number.size();i++)
   {
       if(i != 0 && (number[i] != 0 || i == 4 || i == 8))
          res.push_back(digit[i]);
       res.push_back(n[number[i]]);
   }
   for(int i = res.size() - 1;i >= 0;i--)
   {
       if(i != res.size() - 1)
        cout << " ";
       int zerocnt = 0;
       while( i >= 0 && res[i] == "ling")
       {
           i--;
           zerocnt++;
       }
       if(zerocnt > 0 && res[i] != "Wan")
       {
           cout << "ling ";
       }
       cout << res[i];
   }
}

  

 

以上是关于PAT1082:Read Number in Chinese的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级1082 Read Number in Chinese (25 分)

1082. Read Number in Chinese (25)字符串处理——PAT (Advanced Level) Practise

PAT (Advanced Level) 1082. Read Number in Chinese (25)

1082 Read Number in Chinese (25 分)

1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)