1082. Read Number in Chinese (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1082
题目:

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

分析:
注意100101的情况,在亿或者万之后出现的0需要ling,在亿和万之间或万和各位之内的数字之间的0也需要ling
AC代码:
在他基础上写了注释和省略掉了一部分
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
 freopen("F://Temp/input.txt", "r", stdin);
 string a[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };//存放数字对应的字符串
 string b[] = { "", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi" };//存放位对应的字符串,称作位标识符吧
 vector<string> res;//存放字符串:包括位标示符和其位上的数字
 vector<int> digit;//存放各个位上数字
 int num, e;
 cin >> num;

 if (num == 0){
  cout << "ling";
  return 0;
 }
 else if (num < 0){
  cout << "Fu ";
  num = -num;
 }
 while (num != 0){
  digit.push_back(num % 10);
  num /= 10;
 }
 for (e = 0; e < digit.size() && digit[e] == 0; ++e);//从低位开始找到第一位不等于0的
 //if (e == 8){//如果过亿了,因为最多九位数所以只能是几亿
 //	cout << a[digit[e]] << " Yi";
 //	return 0;
 //}
 for (int i = e; i < digit.size(); ++i){
  if (i != 0 && (digit[i] != 0 || i == 4 || i == 8)){//如果不是个位;位上的数字不是0;是万位或亿位,则放入位标识符
   res.push_back(b[i]);
  }
  res.push_back(a[digit[i]]);//放入位上的数字
 }
 for (int i = res.size() - 1; i >= 0; --i){//倒着来,因为是从小到大放进去,而要从大到小读出来
  if (i != res.size() - 1){
   cout << " ";
  }
  int cnt = 0;
  while (i >= 0 && res[i] == "ling"){//统计连续的0,注意这里万左右的0是不算连续的,因为中间有一个Wan隔着
   --i;
   ++cnt;
  }
  if (cnt != 0 && res[i] != "Wan"){ // 当之前有0,并且当前不是Wan,那么当前就是数字或万或亿,所以要输出ling
   //这里不用担心i会递减到个位为0的情况,因为前面e已经把最后的0的因素给考虑过了,所以最后一位肯定是数字或者是位标示符
   cout << "ling ";
  }
  cout << res[i];
 }
 return 0;
}

截图:

——Apie陈小旭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值