鱼C论坛

 找回密码
 立即注册
查看: 1017|回复: 2

[已解决]求助一下

[复制链接]
发表于 2023-10-19 09:42:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出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



我写的代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;

int main()
{
    string number[]={
        "ling","yi","er","san","si","wu","liu","qi","ba","jiu"
    };
    int num;
    cin>>num;
    if(num<0)
    {
        cout<<"fu";
        num=-num;
    }
    if(num==0)
    {
            cout<<"ling";
        }
        if(num>0)        {
int a=1;int count=0;
                while((num/a) >0)
                {        count++;
                        a=a*10;
               
                }
                while(count!=0)
                {
                        int b;int e;int f;
                        e=count-1;
                        f=pow(10,e);
                        b=num/e;
                        cout<<number[b]<<" ";
                                num=num%f;
                        count--;
                       
               
        }
               
        }
}


打印出来是空,为什么,请问哪里错了
最佳答案
2023-10-19 09:43:23
你的代码有几个问题导致输出为空。首先,你没有包含头文件<string>,所以编译器无法识别string类型。其次,你在计算b的时候使用了错误的公式,应该是b = num / f % 10,而不是b = num / e。最后,你在输出数字拼音之间没有添加空格。以下是修改后的代码:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int main()
{
    string number[] = {
        "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
    };
    int num;
    cin >> num;
    if (num < 0)
    {
        cout << "fu ";
        num = -num;
    }
    if (num == 0)
    {
        cout << "ling";
    }
    if (num > 0)
    {
        int a = 1;
        int count = 0;
        while ((num / a) > 0)
        {
            count++;
            a = a * 10;
        }
        while (count != 0)
        {
            int e = count - 1;
            int f = pow(10, e);
            int b = num / f % 10;
            cout << number[b];
            if (count != 1)
            {
                cout << " ";
            }
            num = num % f;
            count--;
        }
    }
    return 0;
}

这样修改后,你的代码应该能够正确输出数字对应的拼音了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-19 09:43:23 | 显示全部楼层    本楼为最佳答案   
你的代码有几个问题导致输出为空。首先,你没有包含头文件<string>,所以编译器无法识别string类型。其次,你在计算b的时候使用了错误的公式,应该是b = num / f % 10,而不是b = num / e。最后,你在输出数字拼音之间没有添加空格。以下是修改后的代码:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int main()
{
    string number[] = {
        "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
    };
    int num;
    cin >> num;
    if (num < 0)
    {
        cout << "fu ";
        num = -num;
    }
    if (num == 0)
    {
        cout << "ling";
    }
    if (num > 0)
    {
        int a = 1;
        int count = 0;
        while ((num / a) > 0)
        {
            count++;
            a = a * 10;
        }
        while (count != 0)
        {
            int e = count - 1;
            int f = pow(10, e);
            int b = num / f % 10;
            cout << number[b];
            if (count != 1)
            {
                cout << " ";
            }
            num = num % f;
            count--;
        }
    }
    return 0;
}

这样修改后,你的代码应该能够正确输出数字对应的拼音了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-19 09:44:13 | 显示全部楼层
问题分析和修改建议:

1. 你的代码在处理输入整数时存在逻辑错误。例如,对于输入的整数1234,你的代码会将其划分为12、34两个数字进行处理,而实际上你需要按照位数逐个处理每个数字。

2. 在计算数字位数时,你使用了错误的计算方式。应该使用取余操作来获取每个位上的数字,而不是除法。

3. 在输出拼音时,需要注意拼音之间的空格分隔符。

下面是对你的代码进行修正后的版本:
#include <iostream>#include <string>using namespace std;int main() {    string number[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };    int num;    cin >> num;        if (num < 0) {        cout << "fu ";        num = -num;    }        if (num == 0) {        cout << number[0];    }    else {        int digits = 0;        int temp = num;        while (temp > 0) {            temp /= 10;            digits++;        }                int divisor = pow(10, digits - 1);        while (divisor != 0) {            int digit = num / divisor;            cout << number[digit];            if (divisor > 1) {                cout << " ";            }            num %= divisor;            divisor /= 10;        }    }        return 0;}
希望以上解答对你有帮助。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 07:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表