题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
#include <stdio.h> #define YI_1 100000000 #define WAN_1 10000 #define QIAN_1 1000 char* _V[] = {"","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"}; //oFlag 1 千上有值 , 0 千上无值 void printQ(int n,int oFlag) { int qian = n / QIAN_1; int zeroFlag = 0; if(qian > 0) { printf("%s仟",_V[qian]); oFlag = 1; } if(qian == 0 && oFlag) { printf("零"); zeroFlag = 1; } int bai = (n % QIAN_1) / 100; if(bai > 0){ printf("%s佰",_V[bai]); oFlag = 1; zeroFlag = 0; } if(bai == 0 && (0 == zeroFlag) && oFlag) { printf("零"); zeroFlag = 1; } int shi = n % 100; if(shi > 0) { if(shi/10 > 1) printf("%s拾",_V[shi/10]); else if(shi/10 == 1) printf("拾"); if(shi/10 >= 1) zeroFlag = 0; } if(shi == 0 && (0 == zeroFlag) && oFlag) { printf("零"); zeroFlag = 1; } int yuan = n % 10; if(yuan > 0) printf("%s",_V[yuan]); } void printWQ(int n,int oFlag) { int wan = n / WAN_1; if(wan > 0) { printQ(wan,oFlag); printf("万"); } //print q int qian = n % WAN_1; if(qian > 0) { printQ(qian,oFlag); } } void printJF(double d) { int n = d * 10; n = d*1000; if(n%10 == 9) n+=1; n /=10; if(n/10 > 0) printf("%s角",_V[n/10]); //else // printf("零"); int f = n%10; if(f > 0) printf("%s分",_V[f]); } int main() { double money; int n; double jf; int oFlag = 0; scanf("%lf",&money); printf("人民币"); if(money <= 0) { printf("零元"); return 0; } n = (int)money; jf = money - (float)n; //print yi int yi = n/YI_1; if(yi > 0) { oFlag = 1; printWQ(yi,0); printf("亿"); } //print wq int wanq = (n%YI_1); if(wanq > 0) { printWQ(wanq,oFlag); } if(jf <= 0) printf("元整"); else { if(money >= 1.0) printf("元"); printJF(jf); } return 0; }