【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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章