tim day tang co tong lon nhat
//tim day tang co tong lon nhat
#include <stdio.h>
#include <conio.h>
void nhap(float a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("phan tu thu %d : ",i+1);
scanf("%f",&a[i]);
}
}
//ham nhap ptu trong day~
void in(float a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%.0f ",a[i]);
}
//ham in cac ptu
int main()
{
float a[50],tong1,tong2;
int n,i,dau1,dau2,cuoi1,cuoi2;
printf("nhap do dai cua day : ");scanf("%d",&n);
printf("nhap day :
");
nhap(a,n);
printf("day vua nhap la : ");in(a,n);
dau1=dau2=cuoi1=cuoi2=0;
tong1=tong2=a[0];
for(i=1;i<n;i++)
if(a[i-1]>a[i])
{
if(tong1>tong2)
{
dau2=dau1;
cuoi2=cuoi1;
tong2=tong1;
}
dau1=cuoi1=i;
tong1=a[i];
}
else
{
cuoi1++;
tong1+=a[i];
}
if(tong1>tong2)
{
dau2=dau1;
cuoi2=cuoi1;
tong2=tong1;
}
printf("
day tang co tong lon nhat la : ");
for(i=dau2;i<=cuoi2;i++)
printf("%.1f ",a[i]);
printf("
tong la : %.1f",tong2);
getch();
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top