Pagini recente » Cod sursa (job #1517460) | Cod sursa (job #2105001) | Cod sursa (job #970085) | Cod sursa (job #2405148) | Cod sursa (job #1998173)
#include <stdio.h>
#include <stdlib.h>
#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b? a : b)
#define MAX 10001
#define CAP_MAX 10001
int n, GT;
int castig[MAX], greutate[MAX];
int v[2][MAX];
void readInput (){
int i;
FILE *f= fopen("rucsac.in","r");
///printf("Numarul de obiecte: ");
fscanf(f,"%d", &n);
/// printf("Capacitatea rucsacului: ");
fscanf(f,"%d", >);
for (i = 1; i <= n; i++){
///printf("Greutate[%d] = ", i);
fscanf(f,"%d", &greutate[i]);
///printf("Castig[%d] = ", i);
fscanf(f,"%d", &castig[i]);
}
}
int main()
{
readInput();
int i, j, k;
FILE *g= fopen("rucsac.out","w");
for ( i = 0; i <= GT; i++)
v[0][i] = 0;
for ( i = 1; i <= n; i++){
for ( j = 1; j <= GT; j++){
v[1][j] = v[0][j];
if ( greutate[i] <= j )
v[1][j]= max(v[1][j],v[0][j - greutate[i]]+ castig[i]);
}
for ( k = 1; k <= GT; k++)
v[0][k]=v[1][k];
}
fprintf(g,"%d", v[1][GT]);
return 0;
}