不知道哪里修改了变量

liztac 2013-05-16 07:22:54
这是算法导论第三册 动态规划 15.1 别人的一个实现
我为什么在红色字体的地方运行到j=4,i=1时r[0]会被更改呢?j为4啊,不知道什么地方有修改r[0]的语句。
大家知道的,帮我指点一下。

想传张截图上来可是怎么也传不上来,一直卡在上传中

//EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
#include <iostream>
using namespace std;
#include <ctime>
const int MAXISIZE=2000;


void ExtendedBottomUpCutRod(int *p, int n,int *r,int *s)
{
//int r[MAXISIZE];//auxiliary array initialized with minimum

//int s[n];

r[0] = 0;

int i,j,k;
for ( j=1; j<=n; j++)
{
int q = INT_MIN;//??????why q assign so many times
for ( i=1; i<=j; i++)
{
k = p[i-1] + r[j-i]; //p[0] is the price of length 1,so p[0] is 1
if ( q < k )
{
q = k;
s[j]=i;
r[j] = q;
}
}
}
// for(int j=0;j<n;j++)
// cout<<"the r["<<j<<"] is:"<<r[j]<<endl;
}

void PrintCutRodSolution(int* p,int n,int* r,int* s)
{

ExtendedBottomUpCutRod(p,n,r,s);

cout<<"the optimal first sizes are:"<<endl;
for(int i=1;i<=n;i++)
cout<<i<<":"<<s[i]<<" ";
cout<<endl;

cout<<"the optimal value of each length are:"<<endl;
for(int i=1;i<=n;i++)
cout<<i<<":"<<r[i]<<" ";
cout<<endl;

//print the partition
cout<<"Print the partition:";
while(n>0)
{
cout<<s[n]<<" ";
n=n-s[n];
}
cout<<endl;

}

int main()
{
int p[] = {1,5,8,9,10,17,17,20,24,30};
//int p[MAXISIZE] ={1,5,8,9,10,17,17,20,24};

const int rodLength=4;

//r is an array of optimal value of each length
int r[rodLength];
int s[rodLength];

clock_t start = clock();
//int k = MemoizedCutRod(p,1999);
//ExtendedBottomUpCutRod(p,rodLength,r,s);
PrintCutRodSolution(p,rodLength,r,s);

// How can i careless to enter the wrong sequence of parameters
// cout<<"the optimal first sizes are:"<<endl;
//for(int i=1;i<=rodLength;i++)
// cout<<i<<":"<<s[i]<<" ";
//cout<<endl;
//
//
// cout<<"the optimal value of each length are:"<<endl;
//for(int i=1;i<=rodLength;i++)
// cout<<i<<":"<<r[i]<<" ";
//cout<<endl;


clock_t end = clock();
cout<<"optimal value:"<<r[rodLength]<<" "<<"time: "<<double(end-start)/CLOCKS_PER_SEC<<"s"<<endl;

return 0;
}
...全文
86 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liztac 2013-05-17
  • 打赏
  • 举报
回复
“ r[0] = 0; ” 这行算没赋值吗? --------------------- 但是再把 int r[rodLength]; int s[rodLength]; 改成 int r[rodLength+1]; int s[rodLength+1]; 之前,调试的时候,j在运行到最后一轮的时候r[0]会变成非0的值,不知道是什么地方改变的。
chun.zhang 2013-05-17
  • 打赏
  • 举报
回复
“ r[0] = 0; ” 这行算没赋值吗?
ForestDB 2013-05-17
  • 打赏
  • 举报
回复
LZ还知道数组索引从0开始?
liztac 2013-05-16
  • 打赏
  • 举报
回复
是啊。 int r[rodLength]; int s[rodLength]; 改成 int r[rodLength+1]; int s[rodLength+1]; r[0]就没有被更改了。for循环了,j是从1到rodLength的,那么就越界了。但是越界为什么会更改r[0]呢,完全没有更改r[0]的语句啊。
chun.zhang 2013-05-16
  • 打赏
  • 举报
回复
int r[rodLength]; int s[rodLength]; 我没具体看,但是明显你的for循环出错。好好看一下数组知识!
liztac 2013-05-16
  • 打赏
  • 举报
回复
第一次调试了,运行到j=4,i=1时修改的。 现在程序又在main结尾return前才报出stack around variable s was corrupted。 为什么有变成了s呢。好奇怪。
liztac 2013-05-16
  • 打赏
  • 举报
回复
奇怪,重新建了一个控制台项目,运行数据正确了,但是出现stack around variable r was corrupted
derekrose 2013-05-16
  • 打赏
  • 举报
回复
调试一下不就行了 看看运行到哪的时候改的

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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