题解 | #人民币转换#

人民币转换

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

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

vector<string> ones{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"};

string bigNum(int num) {
    if (num <= 10) return ones[num];
    else if (num < 100) {
        return (num / 10 != 1 ? bigNum(num / 10) : "") + "拾" + (num % 10 ? bigNum(num % 10) : "");
    }
    else if (num < 1000) {
        if (num % 100 == 0) {
            return bigNum(num / 100) + "佰";
        }
        else {
            return bigNum(num / 100) + "佰" + ((num % 100) < 10 ? "零" : "") + (bigNum(num % 100));
        }
    }
    else if (num < 10000) {
        if (num % 1000 == 0) {
            return bigNum(num / 1000) + "仟";
        }
        else {
            return bigNum(num / 1000) + "仟" + ((num % 1000) < 100 ? "零" : "") + (bigNum(num % 1000));
        }
    }
    else if (num < 100000000) {
        if (num % 10000 == 0) {
            return bigNum(num / 10000) + "万";
        }
        else {
            return bigNum(num / 10000) + "万" + ((num % 10000) < 1000 ? "零" : "") + (bigNum(num % 10000));
        }
    }

    else {
        if (num % 100000000 == 0) {
            return bigNum(num / 100000000) + "亿";
        }
        else {
            return bigNum(num / 100000000) + "亿" + ((num % 100000000) < 10000000 ? "零" : "") + (bigNum(num % 10000000));
        }
    };
}

string smallNum(int num) {
    if (num == 0) return "整";
    else if (num < 10) return ones[num] + "分";
    else return ones[num / 10] + "角" + (num % 10 ? smallNum(num % 10) : "");
}

int main() {
    int b, s;
    scanf("%d.%d", &b, &s);
    if (b != 0) {
        cout << "人民币" << bigNum(b) << "元" << smallNum(s);
    }
    else {
        cout << "人民币" << smallNum(s);
    }
}

全部评论

相关推荐

联通 技术人员 总包不低于12
点赞 评论 收藏
分享
10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务