CCP-CSP认证考试 201609-1 最大波动 c/c++题解

题目描述

试题编号: 201609-1
试题名称: 最大波动
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  小明正在利用股票的波动程度来研究股票。小明拿到了一只股票每天收盘时的价格,他想知道,这只股票连续几天的最大波动值是多少,即在这几天中某天收盘价格与前一天收盘价格之差的绝对值最大是多少。
输入格式
  输入的第一行包含了一个整数n,表示小明拿到的收盘价格的连续天数。
  第二行包含n个正整数,依次表示每天的收盘价格。
输出格式
  输出一个整数,表示这只股票这n天中的最大波动值。
样例输入
6
2 5 5 7 3 5
样例输出
4
样例说明
  第四天和第五天之间的波动最大,波动值为|3-7|=4。
评测用例规模与约定
  对于所有评测用例,2 ≤ n ≤ 1000。股票每一天的价格为1到10000之间的整数。

题解:

这题一定要注意一个坑点,就是第一天不算波动,意思就是要从第二天开始算

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e3+5;
int n;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    while(cin >> n)
    {
        int last,now;
        int max_b = -inf;
        cin >> last;
        for(int i = 1; i < n; i++)
        {
            cin >> now;
            if(abs(now - last) > max_b)
            {
                max_b = abs(now - last);
            }
            last = now;
        }
        cout << max_b << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值