题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
#include <stdio.h> #include <stdbool.h> #include <math.h> #define LEN 50 void get_bits(double d_n, int * ar); void printInfo(int n, char number[][LEN]); int main(void) { double d_n; static int ar[5]; bool flag = false; char number[13][LEN] = { {"零"}, {"壹"}, {"贰"}, {"叁"}, {"肆"}, {"伍"}, {"陆"}, {"柒"}, {"捌"}, {"玖"}, {"拾"}, {"佰"}, {"仟"} }; char unit[6][LEN] = { {"亿"}, {"万"}, {"元"}, {"角"}, {"分"}, {"整"} }; while (scanf("%lf", &d_n) == 1) { get_bits(d_n, ar); if (ar[0] || ar[1] || ar[2] || ar[3] || ar[4]) printf("人民币"); if (ar[4]) { printInfo(ar[4], number); printf("%s", unit[0]); flag = true; } if (ar[3]) { if (flag && ar[4] % 10 == 0) printf("%s", number[0]); printInfo(ar[3], number); printf("%s", unit[1]); flag = true; } if (ar[2]) { if (flag && ar[3] % 10 == 0) printf("%s", number[0]); printInfo(ar[2], number); flag = true; } if (flag) printf("%s", unit[2]); if (ar[1]) printf("%s%s", number[ar[1]], unit[3]); if (ar[0]) printf("%s%s", number[ar[0]], unit[4]); if (!ar[1] && !ar[0] && flag) printf("%s", unit[5]); } return 0; } void get_bits(double d_n, int * ar) { /* 分 */ double temp = d_n * 100; ar[0] = (long long) round(temp) % 10; /* 角 */ ar[1] = ((long long) round(temp) / 10) % 10; /* 元 */ ar[2] = (long long) d_n % 10000; /* 万 */ ar[3] = ((long long) d_n / 10000) % 10000; /* 亿 */ ar[4] = (long long) d_n / 100000000; } void printInfo(int n, char number[][LEN]) { bool flag = false; if (n >= 1000) { printf("%s%s", number[n / 1000], number[12]); n %= 1000; flag = true; } if (n < 100 && n >= 10 && flag) { printf("%s", number[0]); } else if (n >= 100) { printf("%s%s", number[n / 100], number[11]); n %= 100; flag = true; } if (n < 10 && n > 0 && flag) { printf("%s", number[0]); } else if (n >= 10) { if (n / 10 == 1) printf("%s", number[10]); else printf("%s%s", number[n / 10], number[10]); n %= 10; } if (n > 0) printf("%s", number[n]); }