【PAT】A1082 Read Number in Chinese (25point(s))


Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 400 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB

A1082 Read Number in Chinese (25point(s))

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

Code

#include <bits/stdc++.h>
using namespace std;
map<char,string> num;
map<int,string> unit;
void init(){
    num['1']="yi";num['2']="er";num['3']="san";num['4']="si";
    num['5']="wu";num['6']="liu";num['7']="qi";num['8']="ba";
    num['9']="jiu";num['0']="ling";
    unit[1]="";unit[5]="";unit[9]="";
    unit[2]="Shi";unit[3]="Bai";unit[4]="Qian";
    unit[6]="Shi";unit[7]="Bai";unit[8]="Qian";
}
int main(){
    init();
    string s;
    int flag=0;
    cin>>s;
    s+='0';
    if(s[0]=='-'){
        printf("Fu");
        flag=1;
    }
    for(int i=flag;i<s.size()-1;i++){
        if(i==0){
            cout<<num[s[i]];
            if(i+10!=s.size()&&i+6!=s.size()&&i+2!=s.size())   cout<<" ";
            cout<<unit[s.size()-i-1];
        }
        else{
            if(s[i]!='0'){
                cout<<" "<<num[s[i]];
                if(i+10!=s.size()&&i+6!=s.size()&&i+2!=s.size())   cout<<" ";
                cout<<unit[s.size()-i-1];
            }
            else if(s[i]=='0'&&s[i+1]!='0'){
                cout<<" "<<num['0'];
            }
        }
        if(s.size()-i-1==9) cout<<" Yi";
        if(s.size()-i-1==5) cout<<" Wan";
    }
    return 0;
}

Analysis

-使用中文的习惯输出数字。

注:此代码有问题(目前较忙,日后再改)
测试用例:
100001234
对应输出应该为:
yi Yi ling yi Qian er Bai san Shi si
你的输出为:
yi Yi ling Wan yi Qian er Bai san Shi si

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值