鱼C论坛

 找回密码
 立即注册
查看: 1448|回复: 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。最后,你在输出数字拼音之间没有添加空格。以下是修改后的代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;

  5. int main()
  6. {
  7.     string number[] = {
  8.         "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
  9.     };
  10.     int num;
  11.     cin >> num;
  12.     if (num < 0)
  13.     {
  14.         cout << "fu ";
  15.         num = -num;
  16.     }
  17.     if (num == 0)
  18.     {
  19.         cout << "ling";
  20.     }
  21.     if (num > 0)
  22.     {
  23.         int a = 1;
  24.         int count = 0;
  25.         while ((num / a) > 0)
  26.         {
  27.             count++;
  28.             a = a * 10;
  29.         }
  30.         while (count != 0)
  31.         {
  32.             int e = count - 1;
  33.             int f = pow(10, e);
  34.             int b = num / f % 10;
  35.             cout << number[b];
  36.             if (count != 1)
  37.             {
  38.                 cout << " ";
  39.             }
  40.             num = num % f;
  41.             count--;
  42.         }
  43.     }
  44.     return 0;
  45. }
复制代码


这样修改后,你的代码应该能够正确输出数字对应的拼音了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;

  5. int main()
  6. {
  7.     string number[] = {
  8.         "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"
  9.     };
  10.     int num;
  11.     cin >> num;
  12.     if (num < 0)
  13.     {
  14.         cout << "fu ";
  15.         num = -num;
  16.     }
  17.     if (num == 0)
  18.     {
  19.         cout << "ling";
  20.     }
  21.     if (num > 0)
  22.     {
  23.         int a = 1;
  24.         int count = 0;
  25.         while ((num / a) > 0)
  26.         {
  27.             count++;
  28.             a = a * 10;
  29.         }
  30.         while (count != 0)
  31.         {
  32.             int e = count - 1;
  33.             int f = pow(10, e);
  34.             int b = num / f % 10;
  35.             cout << number[b];
  36.             if (count != 1)
  37.             {
  38.                 cout << " ";
  39.             }
  40.             num = num % f;
  41.             count--;
  42.         }
  43.     }
  44.     return 0;
  45. }
复制代码


这样修改后,你的代码应该能够正确输出数字对应的拼音了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

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

下面是对你的代码进行修正后的版本:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5.     string number[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
  6.     int num;
  7.     cin >> num;
  8.    
  9.     if (num < 0) {
  10.         cout << "fu ";
  11.         num = -num;
  12.     }
  13.    
  14.     if (num == 0) {
  15.         cout << number[0];
  16.     }
  17.     else {
  18.         int digits = 0;
  19.         int temp = num;
  20.         while (temp > 0) {
  21.             temp /= 10;
  22.             digits++;
  23.         }
  24.         
  25.         int divisor = pow(10, digits - 1);
  26.         while (divisor != 0) {
  27.             int digit = num / divisor;
  28.             cout << number[digit];
  29.             if (divisor > 1) {
  30.                 cout << " ";
  31.             }
  32.             num %= divisor;
  33.             divisor /= 10;
  34.         }
  35.     }
  36.    
  37.     return 0;
  38. }
复制代码

希望以上解答对你有帮助。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 05:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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