题解 | #人民币转换#

人民币转换

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

#include <stdio.h>
#include <string.h>

char hanzi[19][4]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾","佰","仟","万","亿","元","角","分","整"};
char wei[19][4]={"整","分","角","元","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};
char zhengwei[4],zhengweiflag=0,lingflag=0;

int main(){
    char str[100];
    int i,len,ling;
    while(scanf("%s",str)!=-1){
        len=strlen(str);
        ling=0;
        zhengweiflag=0;lingflag=0;
        printf("人民币");
        if(str[0]=='0'){
            for(i=0;i<len;i++)
                    if((str[i]!='0')&&(str[i]!='.')) printf("%s%s",hanzi[str[i]-'0'],wei[len-i]);
 
        }
        else 
       { for(i=0;i<len;i++){
            if((str[i]=='0')&&((len-i)!=2)) lingflag=1;
            if(str[i]!='0'){
                if(zhengweiflag) {printf("%s",zhengwei);zhengweiflag=0;}
                if(lingflag) {printf("%s",hanzi[0]);lingflag=0;}
                if((str[i]=='1')&&(((len-i)==5)||((len-i)==9)||((len-i)==13))) printf("拾");
                else {
                    if(((len-i)==4)||((len-i)==8)||((len-i)==12)||((len-i)==3)) printf("%s",hanzi[str[i]-'0']);
                    else
                    printf("%s%s",hanzi[str[i]-'0'],wei[len-i]);
                }
            }
            if(((len-i)==4)||((len-i)==8)||((len-i)==12)){
                lingflag=0;
                if(zhengweiflag==0){
                    zhengweiflag=1;
                    memcpy(zhengwei,wei[len-i],4);
                }
            }
            
        }}
        if((str[len-1]=='0')&&(str[len-2]=='0'))  printf("整");
        printf("\n");
    }

    
    return 0;
}
全部评论
输入:100001000 输出:人民币拾万零壹整
点赞 回复 分享
发布于 2022-02-19 14:49
你这个代码调了多久?
点赞 回复 分享
发布于 2022-08-22 14:43 陕西

相关推荐

11-15 18:39
已编辑
西安交通大学 Java
全村最靓的仔仔:卧槽,佬啥bg呢,本也是西交么
点赞 评论 收藏
分享
1 2 评论
分享
牛客网
牛客企业服务