← Back to topics
Topic

Z-bankar

WindListener
What's wrong with my code:-S

----------------------------------------------------------
#include <iostream>
using namespace std;

int main()
{
int n, K;

scanf( "%d %d", &n, &K );
int niz[n], dp[K];
for( int i = 0; i < n; i++ )
scanf( "%d", &niz[i] );

for( int i = 1; i <= K; i++ )
dp[i] = INT_MAX;

sort( niz, niz+n );

for( int i = 1; i <= K; i++ )
for( int j = 0; j < n; j++ )
if( niz[j] <= i ) min( dp[i], dp[i - niz[j]] + 1 );

if( dp[K] == INT_MAX ) cout << "-1" << endl;
else cout << dp[K] << endl;
system( "pause" );
}
----------------------------------------------------------
matteo123
use printf instead cout.
WindListener
Still not working:-S BTW, what are differents between COUT and PRINTF?
mbalunovic
Printf/Scanf are much faster than cout/cin...
You are getting wrong result, so problem is in your idea or implementation, not reading and printing.
picsel
Have you tried debugging? Problem is obvious. You need to add some things to the code for it to work properly :)
WindListener
I know that...:-) I read about this DP, and try to implement in this code but it doesn't work... :-(
I don't know what's wrong:-( Can u be more precise? :-)
tgudlek


INT_MAX + 1 is INT_MIN :)


#include <iostream>
using namespace std;

int niz[101], dp[50001];

int main()
{
int n, K;

scanf( "%d %d", &n, &K );
for( int i = 0; i < n; i++ )
scanf( "%d", &niz[i] );

for( int i = 1; i <= K; i++ )
dp[i] = 50001;

sort( niz, niz+n );

dp[ 0 ] = 0;

for( int i = 1; i <= K; i++ )
for( int j = 0; j < n && niz[j] <= i ; j++ )
dp[ i ] = min( dp[i], dp[i - niz[j]] + 1 );

if( dp[K] >= 50001 ) cout << "-1" << endl;
else cout << dp[K] << endl;
return 0;
}
WindListener
It is working:-D TNX to all, especially tgudlek...:-)