Code:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
float b,l;
float a[100],f[100];// f[l] là số tiền trả nhỏ nhất sau khi l km
float j,i,temp; //j là vị trí có số tiền trả cho 1km nhỏ nhất, temp là số tiền phải trả đó
void docFile(){
ifstream f1("C:\\BT.INP",ios::in);
f1>>b>>l;
for (i=1;i<l;i++) a[i]=0;
for (int i=1;i<=b;i++) f1>>a[i];
f1.close();
};
void khoiTao(){
f[1]=a[1];
temp=a[1]/1;
j=1;
};
void xuly(){
for (i=1;i<=l;i++){
if (a[i]==0) f[i]=f[j]+f[i-j];
else {
float t=a[i]/i;
if (t>temp){
if (f[j]+j[i-j]<=a[i]) f[i]=f[j]+f[i-j];
else f[i]=a[i];
t=f[i]/i;
if (t<temp){
temp=t;
j=i;
};
}
else{
f[i]=a[i];
temp=t;
j=i;
};
};
};
};
void ghiFile(){
ofstream f2("C:\\BT.OUT",ios::out);
f2<<f[l];
f2.close();
};
void main(){
docFile();
khoiTao();
xuly();
ghiFile();
getch();
};
Nếu đúng, anh cho thêm bài tập nhé.