zoukankan      html  css  js  c++  java
  • 最大销售增幅

    最大销售增幅

    任务描述

    本关任务:编写函数 maxIncrease,用于计算一个销售额序列中的最大销售增幅并返回。这里的销售额都是非负整数。

    对于给定的销售额序列 A,假设序列 A 的长度为 n( n >= 2 ),最大销售额增幅是指满足0 <= x <= y < nA[y] - A[x]的最大值。

    例如,销售额序列11,3,5,7,9,2,4,6,8,10的最大增幅为88(在 x=5 , y=9 时)。

    测试样例

    测试输入:6 12 34 78 45 10 23(第一个数据表示有66天的销售额分,剩下的数据为具体额分)

    预期输出:最大销售增幅为:66

    测试输入:9 90 2 80 3 70 6 50 7 40

    预期输出:最大销售增幅为:78

    源代码

    #include <iostream>
    using namespace std;
    
    // 函数maxIncrease:计算销售额增幅
    // 参数:s-销售额数组,n-销售额数组长度,n>1
    // 返回值:销售额最大增幅
    int maxIncrease(int s[], int n);
    
    int main()
    {
        int n, a[30], i;     // 定义变量及数组,n-销售额个数,a-销售额数组
        cin >> n;      // 输入销售额数量,n>1
        // 输入n个销售额,分别存入a[0]到a[n-1]
        for(i = 0; i < n; i++)
            cin >> a[i];
        i = maxIncrease(a,n);
        cout << "最大销售增幅为:" << i << endl;
        return 0;
    }
    
    int maxIncrease(int s[], int n)
    {
        //请在此添加代码,实现函数maxIncrease
        /********** Begin *********/
    	int i,j,max=s[1]-s[0];
    
        for(i = 0; i <= n-2; i++){
    		for(j=i+1;j<=n-1;j++){
    			if(s[j]-s[i]>max){
    				max=s[j]-s[i];
    			}
    		}
    	}
        return max;
        
        /********** End **********/
    }
    
  • 相关阅读:
    [Luogu] 借教室
    [Luogu] 子共七
    [Luogu] 让我们异或吧
    【bzoj1030】[JSOI2007]文本生成器
    HDU3068 最长回文
    【bzoj2342】[Shoi2011]双倍回文
    【NOIP2012】借教室
    HDU2203 亲和串
    【POJ2001】Shortest Prefixes
    vodevs3031 最富有的人
  • 原文地址:https://www.cnblogs.com/lightice/p/12691828.html
Copyright © 2011-2022 走看看