Skip to content

Instantly share code, notes, and snippets.

@joffilyfe
Created September 10, 2015 20:16
Show Gist options
  • Save joffilyfe/6b26bb0450ea2cadaa2c to your computer and use it in GitHub Desktop.
Save joffilyfe/6b26bb0450ea2cadaa2c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;
int main(){
int np = 4; // quantidade de produtos
int cap = 5; // capacidade da mochila
int precos[] = {10, 15, 20, 12};
int pesos[] = {1, 2, 3, 2};
int matriz[++np][++cap];
for(int i=0; i < cap; i++){
matriz[0][i] = 0;
}
for(int i=0; i < np; i++){
matriz [i][0] = 0;
}
for(int i=1; i < np; i++){
for(int j=1; j < cap; j++){
if(pesos[i] <= j){
matriz[i][j] = precos[i] + matriz[i-1][j-pesos[i]];
//cout << "matriz[i][j] = ";
//cout << "matriz[" << i << "][" << j << "] = " << matriz[i][j] << endl;
//if(matriz[i-1][j] > matriz[i][j])
// matriz[i][j] = matriz[i-1][j];
}else{
matriz[i][j] = matriz[i-1][j];
}
}
}
for(int i=0; i<cap; i++){
for(int j=0; j<np; j++){
cout << setw(3) << matriz[i][j];
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment