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




### 回答1: 当然,我可以用Python来写一个简单的程序来将日元和美元之间进行转换。下面是一个示例程序: ```python # 获取用户输入的金额和货币类型 amount = float(input("请输入金额:")) currency = input("请输入货币类型(USD或JPY):") # 根据货币类型进行转换 if currency == "USD": jpy_amount = amount * 109.08 print("转换结果:{:.2f} 日元".format(jpy_amount)) elif currency == "JPY": usd_amount = amount / 109.08 print("转换结果:{:.2f} 美元".format(usd_amount)) else: print("无效的货币类型,请输入USD或JPY。") ``` 这个程序会首先询问用户要转换的金额和货币类型,然后根据货币类型进行转换并打印出转换结果。在这个示例中,我们使用了一个简单的if-else语句来根据用户输入的货币类型来执行相应的转换计算。转换率是根据当前汇率计算得出的,可能不是最新的汇率,如果需要更精确的结果,需要使用更精确的汇率数据。 ### 回答2: 下面是一个用Python编写的日元和美元币值转换程序的示例代码: ```python def convert_currency(amount, currency): if currency == "JPY": # 将日元转换为美元,汇率为1美元=110日元 converted_amount = amount / 110 return converted_amount elif currency == "USD": # 将美元转换为日元,汇率为1美元=110日元 converted_amount = amount * 110 return converted_amount else: return "无效的货币种类" # 测试程序 print(convert_currency(1000, "JPY")) # 将1000日元转换为美元 print(convert_currency(10, "USD")) # 将10美元转换为日元 ``` 以上代码定义了一个`convert_currency`函数,根据传入的金额和货币种类进行转换。当货币种类为"JPY"时,将日元转换为美元;当货币种类为"USD"时,将美元转换为日元。转换的汇率设定为1美元=110日元。最后,通过调用`convert_currency`函数并传入测试数据进行测试。 运行上述代码,输出结果为: ``` 9.090909090909092 1100 ``` 其中,第一行输出的结果表示将1000日元转换为美元后的金额约为9.09美元;第二行输出的结果表示将10美元转换为日元后的金额为1100日元。 ### 回答3: 下面是一个使用Python编写的日元和美元币值转换程序的示例: ``` # 定义一个函数,将输入的日元金额转换为美元 def jpy_to_usd(jpy_amount): usd_amount = jpy_amount / 100 return usd_amount # 定义一个函数,将输入的美元金额转换为日元 def usd_to_jpy(usd_amount): jpy_amount = usd_amount * 100 return jpy_amount # 主程序 choice = int(input("请选择转换方向:1. 日元转美元 2. 美元转日元:")) if choice == 1: jpy_amount = float(input("请输入要转换的日元金额:")) usd_amount = jpy_to_usd(jpy_amount) print("{} 日元 = {} 美元".format(jpy_amount, usd_amount)) elif choice == 2: usd_amount = float(input("请输入要转换的美元金额:")) jpy_amount = usd_to_jpy(usd_amount) print("{} 美元 = {} 日元".format(usd_amount, jpy_amount)) else: print("输入无效,请选择1或2。") ``` 这个程序首先通过一个选择菜单让用户选择转换方向,然后根据用户的选择别调用对应的函数进行金额转换,并输出转换结果。用户需要别输入要转换的日元金额或美元金额,程序会根据用户的输入进行相应的转换计算,并输出结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值