01knap

 avatar
unknown
c_cpp
3 years ago
912 B
2
Indexable
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define n 4

int max(int a,int b){
	if(a>b)
		return a;
	return b;
}

void formula(int tab[5][9],int *w,int *p,int obj,int weight){

	tab[obj][weight] = max( tab[obj-1][ weight], ( tab[obj-1 ][ (weight-w[obj]) ] + p[obj]) );

}

int main(){
	clrscr();
	int obj[n+1] = {0,1,2,3,4};
	int w[n+1] = {0,2,3,4,5};
	int p[n+1] = {0,1,2,5,6};
	int kSize = 8;
	int tab[5][9],i,j;
	int r=5,c=9;
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			tab[i][j]=0;
		}
	}
	for(i=1;i<r;i++){
		for(j=1;j<c;j++){
			int weight = j,obj = i-1;
			if(obj<0) obj=0;
			tab[i][weight] = max( tab[obj][ weight], ( tab[obj][ (abs(weight-w[i])) ] + p[i]) );
			cout<<tab[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl<<endl;
	for(i=0;i<r;i++){
	  for(j=0;j<c;j++){
		 cout<<tab[i][j]<<" ";
	  }
	  cout<<endl;
	}
	getch();
}
Editor is loading...