thachthen_it Admin
Tổng số bài gửi : 53 Reputation : 0 Join date : 19/05/2011 Age : 32 Đến từ : trà vinh
| Tiêu đề: bài toán balo 1 Mon May 30, 2011 2:16 pm | |
| bài toán này tôi chỉ code thôi về giải thuật trong giáo trình có sẵn: - Code:
-
#include<conio.h> #include<iostream.h> #include<iomanip.h> int a[100],b[50][50],chon[100],m,n,k,v; void Nhap(int a[],int &a); int Max(int a,int b); void Table(); void SearchTable(); void Result(); main() { cout<<"\t\t\tBAI TOAN BALO 1:\n"; Nhap(a,n); Table(); SearchTable(); cout<<"\nKHOI LUONG TUONG UNG DUOC CHON:\n"; Result(); cout<<"\n\t\t_______________________"; } void Nhap(int a[],int &n) { cout<<"nhap so luong mon hang: ";cin>>n; cout<<"khoi trong luong tui : ";cin>>m; for(int i=1;i<=n;i++) { cout<<"\t\tkhoi luong mon thu "<<i<<" la: "; cin>>a[i]; } } int Max(int a,int b) { return a>b?a:b; } void Table() { for(v=1;v<=m;v++) { if(v>=a[1]) b[1][v]=a[1]; else b[1][v]=0; } for(k=2;k<=n;k++) { for(v=1;v<=m;v++) { if(v>=a[k]) b[k][v]=Max(b[k-1][v-a[k]]+a[k],b[k-1][v]); else b[k][v]=b[k-1][v]; } } } void SearchTable() { for(int i=1;i<=n;i++) chon[i]=0; k=n; v=m; do { while((k>=2)&&b[k][v]==b[k-1][v]) k--; chon[k]=1; v=b[k][v]-a[k]; }while(v!=0); } void Result() { for(int i=1;i<=n;i++) if(chon[i]==1) cout<<setw(4)<<a[i]; }[/color] | |
|