背包问题
#include<iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
#define MAXSIZE 1000
int A[MAXSIZE+1][MAXSIZE+1],B[MAXSIZE+1][MAXSIZE+1];
int c[MAXSIZE+1],w[MAXSIZE+1];
int F(int n,int v)
{
if(n==0) return 0;
if( !A[n][v] && v >= c[n])
A[n][v] = F(n-1,v-c[n]) +w[n];
if(!B[n][v])
B[n][v] = F(n-1,v);
return A[n][v]>B[n][v]?A[n][v]:B[n][v];
}
int main()
{ int n,v;
ofstream examplefile ("example");
if (examplefile.is_open()) {
examplefile << "This is a line.\n";
examplefile << "This is another line.\n";
examplefile.close();
}
ifstream in("1" ,ios_base::binary);
ofstream out("2.txt");
in>>n>>v;
for(int i=1;i<=n;i++)
in>>c[i]>>w[i];
out<<F(n,v);
out.flush();
out.close();
cin>>n;
return 0;
}
#include <fstream>
#include <stdlib.h>
using namespace std;
#define MAXSIZE 1000
int A[MAXSIZE+1][MAXSIZE+1],B[MAXSIZE+1][MAXSIZE+1];
int c[MAXSIZE+1],w[MAXSIZE+1];
int F(int n,int v)
{
if(n==0) return 0;
if( !A[n][v] && v >= c[n])
A[n][v] = F(n-1,v-c[n]) +w[n];
if(!B[n][v])
B[n][v] = F(n-1,v);
return A[n][v]>B[n][v]?A[n][v]:B[n][v];
}
int main()
{ int n,v;
ofstream examplefile ("example");
if (examplefile.is_open()) {
examplefile << "This is a line.\n";
examplefile << "This is another line.\n";
examplefile.close();
}
ifstream in("1" ,ios_base::binary);
ofstream out("2.txt");
in>>n>>v;
for(int i=1;i<=n;i++)
in>>c[i]>>w[i];
out<<F(n,v);
out.flush();
out.close();
cin>>n;
return 0;
}