-
March 26th, 2022, 06:42 AM
#1
Basic Dynamic Programming code
Hello
i'm just trying to do some basic example of dynamic programming and i've an issue with the following code:
Code:
#include <cstdio>
#include <vector>
#include <ctime>
using namespace std;
int cut_rod(vector<int>p,int l){
int m;
if (l==0) return 0;
int q=-32768;
for (int i=1;i<=p.size();i++){
if ((l-i)<0) continue;
m=p[i-1]+cut_rod(p,l-i);
if (q<m) q=m;
}
return q;
}
int main(){
int n,t;
int tstart;
int tend;
tstart=clock();
scanf("%i %i\n ",&n,&t);
int q;
int l;
vector<int> p;
for (int j=0;j<n;j++){
p.push_back(0);
scanf("%i",&p[j]);
}
for (int i=0;i<t;t++){
scanf("%i",&l);
q=cut_rod(p,l);
printf("Case %i: %i \n",i,q);
}
tend=clock();
printf("Elapsed time : %i",tend-tstart);
return 0;
}
The program go crazy also for examples with few iteration like:
What's the problem?
Last edited by Quasar999; March 26th, 2022 at 10:03 AM.
-
March 26th, 2022, 11:36 AM
#2
Re: Basic Dynamic Programming code
Use the debugger to trace through the code and watch the variables. When the debugger shows behaviour different from that expected from the design then you have found an issue.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 28th, 2022, 09:38 AM
#3
Re: Basic Dynamic Programming code
In addition to what 2kaud said, you should learn to write readable code. Single character variable names, and multiple statements on the same line make for code that's both hard to read and debug.
-
March 28th, 2022, 10:09 AM
#4
Re: Basic Dynamic Programming code
... and also a mix of C++ and C !
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 9th, 2022, 03:03 PM
#5
Re: Basic Dynamic Programming code
thanks for this, I had a similar doubt with this one. I also want my kid to learn coding that is why I'm planning to enroll him to QRangers and take the Coding For Kids course.
Last edited by MIa White; May 1st, 2022 at 10:55 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|