Problema rucsacului
#include<iomanip>Fisier
#include<iostream>
#include<fstream>
using namespace std;
int n,C,m[100][100],s[100],v[100];
void citire()
{int i,j;
ifstream f("date");
f>>n;
for(i=1;i<=n;i++)
f>>s[i]>>v[i];
f>>C;}
void afisare()
{int i,j;
for(i=1;i<=n;i++)
{ for(j=1;j<=C;j++)
cout<<setw(3)<<m[i][j];
cout<<endl;}
}
int maxim(int a,int b)
{if(a>b)return a;
else return b;
}
int main()
{citire();
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=C;j++)
if(s[i]==j || m[i-1][j-s[i]]!=0)
m[i][j]=maxim(m[i-1][j],m[i-1][j-s[i]]+v[i]);
else m[i][j]=m[i-1][j];
afisare();
}
5
3 6
2 4
4 6
1 5
6 18
10