7-25 念数字(15 分)Java与C++

7-25 念数字(15 分)
输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
输入格式:

输入在一行中给出一个整数,如:1234。

提示:整数包括负数、零和正数。

输出格式:

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。

输入样例:

-600
输出样例:

fu liu ling ling

解题思路1:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int i=0;
        String str=String.valueOf(a);
        String [] array=new String[]{"ling","yi","er","san","si","wu","liu","qi","ba","jiu","fu"};
        for(;i<str.length()-1;i++){
            char c=str.charAt(i);
            if(c=='-'){
                System.out.print(array[10]+" ");
            }else if(c=='0'){
                System.out.print(array[0]+" ");
            }
            else if (c=='1'){
                System.out.print(array[1]+" ");
            }else if(c=='2'){
                System.out.print(array[2]+" ");
            }else if(c=='3'){
                System.out.print(array[3]+" ");
            }else if(c=='4'){
                System.out.print(array[4]+" ");
            }else if(c=='5'){
                System.out.print(array[5]+" ");
            }else if(c=='6'){
                System.out.print(array[6]+" ");
            }else if(c=='7'){
                System.out.print(array[7]+" ");
            }else if(c=='8'){
                System.out.print(array[8]+" ");
            }else if(c=='9'){
                System.out.print(array[9]+" ");
            }
        }
        if(i==str.length()-1){
            char c=str.charAt(i);
            if(c=='-'){
                System.out.print(array[10]);
            }else if(c=='0'){
                System.out.print(array[0]);
            }
            else if (c=='1'){
                System.out.print(array[1]);
            }else if(c=='2'){
                System.out.print(array[2]);
            }else if(c=='3'){
                System.out.print(array[3]);
            }else if(c=='4'){
                System.out.print(array[4]);
            }else if(c=='5'){
                System.out.print(array[5]);
            }else if(c=='6'){
                System.out.print(array[6]);
            }else if(c=='7'){
                System.out.print(array[7]);
            }else if(c=='8'){
                System.out.print(array[8]);
            }else if(c=='9'){
                System.out.print(array[9]);
            }
        }

    }
}

解题思路2:

#include <cstdio>
#include <cstring>
using namespace std;
char num[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
char a[1005];
int main(){
    scanf("%s", a);
    if( a[0] == '-' ) {
        printf("fu");
    }else {
        printf("%s", num[a[0] - '0']);  // 输出数字对应的拼音
    }
    for( int i = 1; i < strlen(a); i++ ) {
        printf(" %s", num[a[i] - '0']);  // 行末没有最后的空格
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值