thu huong Moderator
Tổng số bài gửi : 25 Registration date : 17/09/2007
| Tiêu đề: DAY CON DAI NHAT 22/4/2008, 07:03 | |
| PHUONG PHAP QUI HOACH DONG. - Code:
-
#include <iostream> #include <conio.h> #include <fstream>
using namespace std;
int a[100];//day ban dau int b[100];//day con dai nhat trong day co i phan tu dau tien a[1]...a[i] int c[100];//la day con khong giam dai nhat im duoc int truoc[100];//la chi so cua phan u i(cac phan u giu lai rong c
int i,j,bmax,chiso,n,chisomax;
void nhap() { ifstream fi; fi.open("data.txt"); fi>>n; for(int i=0;i<n;i++) fi>>a[i]; fi.close(); } void Taobang() { b[0]=1; for(i=1;i<n;i++) { bmax=0; for(j=i-1;j>=0;j--) if((a[j]<=a[i])&&(b[j]>bmax)) { bmax=b[j]; chiso=j; } b[i]=bmax+1; truoc[i]=chiso; } } void Trabang() { chisomax=n; for(i=n-1;i>1;i--) { if(b[i]>b[chisomax]) chisomax=i; chiso=chisomax; for(i=b[chisomax];i>=1;i--) { c[i]=a[chiso]; chiso=truoc[chiso]; } } } void xuat() { ofstream fo; fo.open("out.txt"); fo<<"day co dai nhat la: "<<endl; for(i=1;i<=b[chisomax];i++) fo<<c[i]<<" "; fo.close(); } void main() { nhap(); Taobang(); Trabang(); xuat(); getch(); }
code http://groups.google.com.vn/group/hoc-tap-tink31/files?hl=vi&upload=1 | |
|