基础编程题目集 7-25 念数字 (15分)

在这里插入图片描述

c:

#include <stdio.h>
int main()
{
    int n, h, k = 0, p, y, a[107], i = 0;
    scanf("%d", &n);
    if (n == 0)
    {
        printf("ling");
        return 0;
    }
    if (n < 0)
    {
        printf("fu ");
        n = -n;
    }
    h = n;
    y = n;
    while (h > 0)
    {
        h = h / 10;
        k++;
    }
    for (p = 1; p <= k; p++)
    {
        a[i] = y % 10;
        y = y / 10;
        i++;
    }
    for (i = k - 1; i >= 0; i--)
    {

        if (a[i] == 0)
            printf("ling");
        else if (a[i] == 1)
            printf("yi");
        else if (a[i] == 2)
            printf("er");
        else if (a[i] == 3)
            printf("san");
        else if (a[i] == 4)
            printf("si");
        else if (a[i] == 5)
            printf("wu");
        else if (a[i] == 6)
            printf("liu");
        else if (a[i] == 7)
            printf("qi");
        else if (a[i] == 8)
            printf("ba");
        else
            printf("jiu");
        if (i != 0)
            printf(" ");
    }
    return 0;
}

c++:

#include <iostream>
#include <string>
using namespace std;
string mp[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main()
{
    string str;
    cin >> str;
    if (str[0] == '-')
        cout << "fu ", str = str.substr(1);
    for (int i = 0; i < str.size(); i++)
    {
        if (i)
            cout << " ";
        cout << mp[str[i] - '0'];
    }
    return 0;
}
发布了287 篇原创文章 · 获赞 117 · 访问量 8925

猜你喜欢

转载自blog.csdn.net/qq_44458489/article/details/105400305