JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
need help on this pseudocode | Reply
I tried to implement this pseudocode for min no of coins but i m not getting right answer for sum=4, 6,7 and when i put sum=10 , the program stop responding.

pseudocode-
Set Min[i] equal to Infinity for all of i
Min[0]=0

For i = 1 to S
For j = 0 to N - 1
If (Vj<=i AND Min[i-Vj]+1<Min[i])
Then Min[i]=Min[i-Vj]+1

Output Min[S]

#include><iostream>
using namespace std;
int main()
{
int i,j,sum,coins[]={1,3,5}, min[sum];
cin>>sum;
for(i=0;i<=sum;i++)
min[i]=99;
min[0]=0;
for(i=1;i<=sum;i++)
{
for(j=0;j<3;j++)
{
if( ( coins[j]<=i) && (min[ i - coins [ j ] ] + 1 < min[i] ) )
{

min[i]=min[i-coins[j]] +1;
// cout<<endl;
}
//cout><<"im out of if condition";
}
}
cout<<min[sum];
return 0;
}

what am i doing wrong..>
Re: need help on this pseudocode (response to post by codingnoob88) | Reply
//i don't know why did it work at first i think it should be
#include<iostream>
using namespace std;
int main()
{
// here are the changes
int i,j,sum,coins[]={1,3,5};
cin>>sum;
int min[sum+1];
// end of changes
for(i=0;i<=sum;i++)
min[i]=99;
min[0]=0;
for(i=1;i<=sum;i++)
{
for(j=0;j<3;j++)
{
if( ( coins[j]<=i) && (min[ i - coins [ j ] ] + 1 < min[i] ) )
{

min[i]=min[i-coins[j]] +1;
// cout<<endl;
}
//cout><<"im out of if condition";
}
}
cout<<min[sum];
return 0;
}>
RSS