题解 | #人民币转换#

人民币转换

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);
    }
}

全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
09-27 14:42
已编辑
浙江大学 Java
未来未临:把浙大放大加粗就行
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务