高手帮看下这道题为什么不能输出

longbeing 2011-10-17 03:31:36
#include <iostream>
using namespace std;
void main()
{
int n;
int pData[]={1,-2,3,4,5,-7};
int p=sizeof(pData)/sizeof(pData[0]);
bool FindGreatestSumOfSubArray(int pData[],unsigned int p, int n);

cout<<n<<endl<<p<<endl;


}

bool FindGreatestSumOfSubArray
(
int *pData, // an array
unsigned int nLength, // the length of array
int &nGreatestSum // the greatest sum of all sub-arrays
)
{
// if the input is invalid, return false
if((pData == NULL) || (nLength == 0))
return false;

int nCurSum = nGreatestSum = 0;
for(unsigned int i = 0; i < nLength; ++i)
{
nCurSum += pData[i];

// if the current sum is negative, discard it
if(nCurSum < 0)
nCurSum = 0;

// if a greater sum is found, update the greatest sum
if(nCurSum > nGreatestSum)
nGreatestSum = nCurSum;

}


// if all data are negative, find the greatest element in the array
if(nGreatestSum == 0)
{
nGreatestSum = pData[0];
for(unsigned int i = 1; i < nLength; ++i)
{
if(pData[i] > nGreatestSum)
nGreatestSum = pData[i];
}
}

return true;
}

...全文
41 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
科比布莱恩特 2011-10-17
  • 打赏
  • 举报
回复
FindGreatestSumOfSubArray也没有调用,只有声明,
科比布莱恩特 2011-10-17
  • 打赏
  • 举报
回复
程序都写得有问题。

n没有初始化,p的值永远为1,
longbeing 2011-10-17
  • 打赏
  • 举报
回复
谢谢~
ouyh12345 2011-10-17
  • 打赏
  • 举报
回复
void main()
{
int n;
int pData[]={1,-2,3,4,5,-7};
int p=sizeof(pData)/sizeof(pData[0]);
bool FindGreatestSumOfSubArray(int pData[],unsigned int p, int n);

cout<<n<<endl<<p<<endl;


}

声明了FindGreatestSumOfSubArray函数,但没有调用它
luciferisnotsatan 2011-10-17
  • 打赏
  • 举报
回复
main里只声明了
bool FindGreatestSumOfSubArray(int pData[],unsigned int p, int n);
又没调用该函数。

64,682

社区成员

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

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