题解 | #人民币转换#

人民币转换

https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

#include <iostream>
using namespace std;
#include<string>

string zh[10] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
string unit[17] = {"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "万"};

void before_point(string s) {
    if(s=="0")
    {
        return;
    }
    int len = s.size() - 1;
    if (!(len % 4 == 1 && s[0] == '1')) { //防止出现壹拾伍这样的数,
        cout << zh[s[0] - '0'];
    }
    cout << unit[len];
    for (int i = 1; i <= len; i++) {
        if ((len - i) % 4 == 0 &&
                s[i] == '0') { //万,亿这些位置为0时,需要加上单位万或者亿等
            cout << unit[len - i];
            continue;
        }
        if (s[i] != '0') { //如果是字符串中的数字,一定要加上''!!!!
            if (s[i - 1] == '0'&&((len-i)!=3)) {//十万以上的数字接千不用加“零”,例如,30105000.00应写作“人民币叁仟零拾万伍仟元整”
                cout << "零";
            }
            if (!((len-i) % 4 == 1 && s[i] == '1')) { //防止出现壹拾伍这样的数,
                cout << zh[s[i] - '0'];
            }
            cout << unit[len - i];
        }
    }
    cout << "元";
}

void after_point(string s) {
    if (s == "00") {
        cout << "整";
        return;
    }   
    if (s[0] != '0') {
        cout << zh[s[0] - '0']; //5.03 输出5元三分
        cout << "角";
    }
    if (s[1] == '0') {
        return;
    } else {
        cout << zh[s[1] - '0'];
        cout << "分";
    }
}


int main() {
    string s;
    while (getline(cin, s)) {
        string s1 = s.substr(0, s.find('.'));
        string s2 = s.substr(s.find('.') + 1);
        cout << "人民币";
        before_point(s1);
        after_point(s2);
        cout << endl;
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 12:11
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 11:30
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务