代码改变世界

1082 Read Number in Chinese (25分)

2020-02-27 20:57  -三余无梦生  阅读(...)  评论(...编辑  收藏
// 1082.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string str;
string s1[4] = { "Ge","Shi","Bai","Qian" };
string s2[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
vector<string> rs, t;
int main()
{
    int n;
    cin >> n;
    if (n<0)
        cout << "Fu ";
    else if (n == 0) {
        cout << "ling"; /*一个零特判*/
        return 0;
    }
    str = to_string(abs(n));
    int len = str.size();
    for (int i = 0; i < len; i++) {
        rs.push_back(s2[str[i] - '0']);
        int pos = len - i - 1;
        if (pos % 4 && str[i] != '0') /* 0十->0*/
            rs.push_back(s1[pos % 4]);
        if (pos == 4)
            rs.push_back("Wan"); /*到万位加万,亿位加亿*/
        if (pos == 8)
            rs.push_back("Yi");
    }
    for (int i = 0; i < rs.size(); i++) {
        t.push_back(rs[i]);
        if (i != 0 && rs[i] == "ling"&&rs[i - 1] == "ling") /*删除连续的零为一个零*/
            t.pop_back();
    }
    t.push_back("Wan");
    for (int i = 0; i < t.size()-1; i++) {
        if (t[i] == "ling" && t[i + 1] == "Wan" )/*删除单个不需要的零如有1000->yi qian ling->yi qian*/
            continue;
        if (i != 0)
            cout << " ";
        cout << t[i];
    }
    return 0;
}