5-23 币值转换 (20分)

5-23 币值转换   (20分)

输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。

输入格式:

输入在一行中给出一个不超过9位的非负整数。

输出格式:

在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯。

输入样例1:

813227345

输出样例1:

iYbQdBcScWhQdBeSf

输入样例2:

6900

输出样例2:

gQjB

//开始看到题目后,十分纠结,不知该如何下手,没去做,后来才做,虽然代码优化度低,最起码能AC,比不做强,遇到题目要敢于做,不要畏手畏脚......

//另外,个人感觉此题不错,但题干透露信息少,许多人民币转化成财务要求的格式未给出,靠自己琢磨,不断Wa......最后AC的结果,我不满意,例如,100000001,我认为是一亿零一,可不对,按照一亿零零一,对了......程序判断和个人认知有出入啊......

自认为对的程序:

#include <stdio.h>
#include <stdlib.h>
char item[11]="abcdefghij";
char loca[5]="QBSA";
void transport(int num[],int n)
{
    int location=0;
    while(num[location]==0&&location<n)
         location++;
    if(num[location-1]==0&&location!=0&&num[location]!=0)
        printf("%c",item[0]);
    while(location<=n){

        if(num[location]!=0&&location<=n){

            if(location!=n)
                printf("%c%c",item[num[location]],loca[4-n-1+location]);
            else
                printf("%c",item[num[location]]);


        }
        else if(num[location]==0&&num[location+1]!=0&&location<=n-1)
            printf("%c",item[0]);
        location++;


    }
    return;

}
int main()
{
    char p[10];

    char ch;
    int num[9];
    int i=0;
    while((ch=getchar())!='\n'){
        p[i]=ch;
        i++;

    }
    p[i]='\0';
    int length=strlen(p);
    for(i=0;i<=length-1;i++)
        num[i]=p[i]-'0';
    if(length<=4)
       transport(num,length-1);
    else if(length<=8){
        transport(num,length-5);
        printf("W");
        transport(num+length-4,3);
    }
    else{
        printf("%cY",item[num[0]]);
        transport(num+1,3);
        if(num[1]!=0||num[2]!=0||num[3]!=0||num[4]!=0)
            printf("W");
        //if(num[length-4]==0)
           //printf("%c",item[0]);
        transport(num+5,3);

    }

    return 0;
}
程序判断对的:

#include <stdio.h>
#include <stdlib.h>
char item[11]="abcdefghij";
char loca[5]="QBSA";
void transport(int num[],int n)
{
    int location=0;
    while(num[location]==0)
            location++;
    if(num[location-1]==0&&location!=0)
                printf("%c",item[0]);
    while(location<=n)
    {

        if(num[location]!=0&&location<=n)
        {

            if(location!=n)
            printf("%c%c",item[num[location]],loca[4-n-1+location]);
            else
                printf("%c",item[num[location]]);


        }
        else if(num[location]==0&&num[location+1]!=0&&location<=n-1)
            printf("%c",item[0]);
        location++;


    }
    return;

}
int main()
{
    char p[10];

    char ch;
    int num[9];
    int i=0;
    while((ch=getchar())!='\n')
    {
        p[i]=ch;
        i++;

    }
    p[i]='\0';
    int length=strlen(p);
    for(i=0;i<=length-1;i++)
        num[i]=p[i]-'0';
    if(length<=4)
       transport(num,length-1);
    else if(length<=8)
    {
        transport(num,length-5);
        printf("W");
        transport(num+length-4,3);
    }
    else
    {
        printf("%cY",item[num[0]]);
        transport(num+1,3);
        if(num[1]!=0||num[2]!=0||num[3]!=0||num[4]!=0)
            printf("W");
        if(num[length-4]==0)
           printf("%c",item[0]);
        transport(num+5,3);

    }

    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值