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 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

题目大意:给出一个数,用中文给读出来,并且0要正确处理。

//我一看见这个题就蒙了,完全没有思路。

代码来自:https://www.liuchuo.net/archives/2204

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string num[] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string c[] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
int J[] = {, , , , , , , , };
vector<string> res;
int main() {
int n;
cin >> n;
if (n == ) {
cout << "ling";
return ;
}
if (n < ) {
cout << "Fu ";
n = -n;
}
int part[];
part[]= n / ;//亿以上的
part[]= (n % ) / ;//千万级
part[] = n % ;//万级
bool zero = false; //是否在非零数字前输出合适的ling
int printCnt = ; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
for (int i = ; i < ; i++) {
int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
for (int j = ; j >= ; j--) {
int curPos = - i * + j; //当前数字的位置
if (curPos >= ) continue; //最多九位数
int cur = (temp / J[j]) % ;//取出当前数字
if (cur != ) {
if (zero) {
printCnt++ == ? cout<<"ling" : cout<<" ling";
zero = false;
}
if (j == )
printCnt++ == ? cout << num[cur] : cout << ' ' << num[cur]; //在个位,直接输出
else
printCnt++ == ? cout << num[cur] << ' ' << c[j] : cout << ' ' << num[cur] << ' ' << c[j]; //在其他位,还要输出十百千
} else {
if (!zero&&j != && n / J[curPos] >= ) zero = true; //注意100020这样的情况
}
}
if (i != && part[i]>) cout << ' ' << c[i + ]; //处理完每部分之后,最后输出单位,Yi/Wan
}
return ;
}

//这个逻辑太难了。我是写不出来的,勉强看懂。

PAT 1082 Read Number in Chinese[难]的更多相关文章

  1. PAT 1082. Read Number in Chinese

    #include <cstdio> #include <cstdlib> #include <string> #include <vector> #in ...

  2. 1082 Read Number in Chinese (25 分)

    1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...

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

    模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  4. 【PAT甲级】1082 Read Number in Chinese (25 分)

    题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. AAAAAccepted ...

  5. 1082. Read Number in Chinese (25)

    题目如下: Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch ...

  6. PTA (Advanced Level)1082.Read Number in Chinese

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...

  7. 1082 Read Number in Chinese

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...

  8. 1082. Read Number in Chinese (25)-字符串处理

    题意就是给出9位以内的数字,按照汉子的读法读出来. 读法请看下方的几个例子: 5 0505 0505 伍亿零伍佰零伍万零伍佰零伍 5 5050 5050 伍亿伍仟零伍拾万伍仟零伍拾  (原本我以为这个 ...

  9. 1082 Read Number in Chinese (25分)

    // 1082.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string> #include <vecto ...

随机推荐

  1. 了解 Go 1.9 的类型别名

    http://colobu.com/2017/06/26/learn-go-type-aliases/

  2. 制作SD卡启动自己编译的uboot.bin

    README for FriendlyARM Tiny4412 -----------------------------------------------------1. Build uboot ...

  3. oracle中REF Cursor用法

    from:http://www.111cn.net/database/Oracle/42873.htm 1,什么是 REF游标 ? 动态关联结果集的临时对象.即在运行的时候动态决定执行查询. 2,RE ...

  4. 上千万或上亿数据(有反复),统计当中出现次数最多的N个数据. C++实现

    上千万或上亿的数据,如今的机器的内存应该能存下.所以考虑採用hash_map/搜索二叉树/红黑树等来进行统计次数. 然后就是取出前N个出现次数最多的数据了,能够用第2题提到的堆机制完毕. #inclu ...

  5. raw_input()

    raw_input() 用于接收标准输入,并把标准输入当成字符串类型来处理,只能在 Python2 中使用,Python3 中没有这个函数 #!/usr/bin/env python #-*- cod ...

  6. Tomcat在Linux下的安装与配置

    一.安装配置JDK 1.官网下载JDK1.7 mkdir /usr/java cd /uar/java wget http://download.oracle.com/otn/java/jdk/7u8 ...

  7. 使用Node.js完成路由

    首先先看一下文件的结构: 我想通过改变不同的路由进不同的页面, 先看这几个HTML页面: 404: <!DOCTYPE html> <html lang="en" ...

  8. c语言基础知识要点

    C语言程序的构成 与C++.Java相比,C语言其实很简单,但却非常重要.因为它是C++.Java的基础.不把C语言基础打扎实,很难成为程序员高手. 一.C语言的结构 先通过一个简单的例子,把C语言的 ...

  9. MySql学习—— 查询性能优化 深入理解MySql如何执行查询

    本篇深入了解查询优化和服务器的内部机制,了解MySql如何执行特定查询,从中也可以知道如何更改查询执行计划,当我们深入理解MySql如何真正地执行查询,明白高效和低效的真正含义,在实际应用中就能扬长避 ...

  10. c++11——可变参数模板

    在c++11之前,类模板和函数模板只能含有固定数量的模板参数,c++11增加了可变模板参数特性:允许模板定义中包含0到任意个模板参数.声明可变参数模板时,需要在typename或class后面加上省略 ...