在做pat1005时,有点问题求指点

qq_40876293 2018-08-29 03:46:35
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10
​100
​​ ).

Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345
Sample Output:
one five



我写的————————————————————————————————


#include<iostream>
#include<string>
using namespace std;
#define MAX_ 420
int * fen(int n);
int main() {
int *a;
int n = 0;
int sum = 0;
int shu;
cin >> shu;
a=fen(shu);
for (int i = a[0]; i > 0; i--)
sum = sum + a[i];
a = fen(sum);
string zi[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
for (int i = a[0]; i > 0; i--)//输出相应的字符
{
if (i == 1)
cout << zi[a[i]];
else
cout << zi[a[i]] << " ";
}
return 0;
}
int * fen(int n) //函数目的是把数变成单个数字分别存在数组中,shu【0】为数字位数
{
int *b;
int shu[MAX_];
int i=0;
if (n == 0)
{
shu[1] = 0;
shu[0] = 1;
b = shu;
return b;
}
while (n != 0) {
i++;
shu[i] = n % 10;
n = n / 10;
}
shu[0] = i;
b = shu;
return b;
}


程序在MAX_小于大约500时不能正常运行,请问是怎么回事
...全文
153 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-08-30
  • 打赏
  • 举报
回复
边界条件
输入输出格式
……

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
  • 打赏
  • 举报
回复
没看明白你说的程序的问题是什么,不过,调试跟中肯定能解决。这是必备技能。
cjzzmdn 2018-08-30
  • 打赏
  • 举报
回复
函数:shu返回局部变量或临时变量的地址
是不对的吧?

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧