【问题描述】
编写函数 maxIncrease,用于计算一个销售额序列中的最大销售增幅。这里的销售额都是非负整数。
对于给定的销售额序列 A,假设序列 A 的长度为 n( n >= 2 ),最大销售额增幅是指满足0 <= x <= y < n的A[y] - A[x]的最大值。
例如,10天的销售额序列11,3,5,7,9,2,4,6,8,10的最大增幅为8(在 x=5 , y=9 时)。
【输入形式】
10 11 3 5 7 9 2 4 6 8 10(第一个数据表示有 10 天的销售额)
【输出形式】
8
【样例输入】
4 0 89 190 289
【样例输出】
289
#include <stdio.h>
#include <stdlib.h>
// 函数maxIncrease:计算销售额增幅
// 参数:s-销售额数组,n-销售额数组长度,n>1
// 返回值:销售额最大增幅
int maxIncrease(int s[], int n);
int main()
{
int n,a[30],i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("%d\n",maxIncrease(a,n));
return 0;
}
int maxIncrease(int s[], int n)
{int x,y,max,m;
max=0;
for(x=0;x<n-1;x++)
for(y=x+1;y<n;y++)
{m=s[y]-s[x];
if(max<m)
max=m;
}
return max;
}