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

输入描述:

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>
#include <vector>
using namespace std;
string num[10] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string c[6] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
int J[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};

int main()
{
    freopen("in.txt","r",stdin);
    int n;
    cin >> n;
    if (n == 0)
    {
        cout << "ling";
        return 0;
    }
    if (n < 0)
    {
        cout << "Fu ";
        n = -n;
    }
    int part[3];
    part[0]= n / 100000000;
    part[1]= (n % 100000000) / 10000;
    part[2] = n % 10000;
    bool zero = false; //是否在非零数字前输出合适的ling
    int printCnt = 0; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
    for (int i = 0; i < 3; i++)
    {
        int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
        for (int j = 3; j >= 0; j--)
        {
            int curPos = 8 - i * 4 + j; //当前数字的位置
            if (curPos >= 9) continue; //最多九位数
            int cur = (temp / J[j]) % 10;//取出当前数字
            if (cur != 0)
            {
                if (zero)
                {
                    printCnt++ == 0 ? cout<<"ling" : cout<<" ling";
                    zero = false;
                }
                if (j == 0)
                    printCnt++ == 0 ? cout << num[cur] : cout << ‘ ‘ << num[cur]; //在个位,直接输出
                else
                    printCnt++ == 0 ? cout << num[cur] << ‘ ‘ << c[j] : cout << ‘ ‘ << num[cur] << ‘ ‘ << c[j]; //在其他位,还要输出十百千
            }
            else
            {
                if (!zero&&j != 0 && n / J[curPos] >= 10) zero = true;   //注意100020这样的情况,0最小也要从10位开始才读,10位之前如果有0要读出来,肯定n/j[curpos]>=10
            }
        }
        if (i != 2 && part[i]>0) cout << ‘ ‘ << c[i + 4]; //处理完每部分之后,最后输出单位,Yi/Wan
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kuroko-ghh/p/10289890.html

时间: 2024-07-30 16:20:51

Read Number in Chinese (25)的相关文章

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

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

题目信息 1082. Read Number in Chinese (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(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 B

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 b

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

模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

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

题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int num[17];string united[17]={"","yi","er","san&qu

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 read it in the traditional Chinese way. Output Fufirst if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan li

PAT1082. Read Number in Chinese

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 Sh

PAT 1082. Read Number in Chinese

#include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <algorithm> using namespace std; const char* d2cn[] = {"ling", "yi", "er", "san", "si", "