1. 程式人生 > >Day 14: Scope

Day 14: Scope

題目說明:


題目解讀:

給定一個整形陣列,找出任意兩個元素的最大絕對值

解題思路:

找出最大值和最小值即可,如果不限制可以直接使用排序函式sort

示例程式碼:

// scope.cpp: 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <windows.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

class Difference
{
public:
    Difference( vector<int>a )
    {
        this->ele = a;
    }
    ;

private:
    vector<int> ele;

public:
    int maxNum;
    int computeDifference()
    {
        sort( ele.begin(), ele.end() );
        this->maxNum = abs( ele[0] - ele[ele.size() - 1] );
        return this->maxNum;
    }
};

//by zhaocl

int main()
{
    int n;
    cin >> n;
    vector<int> arr;

    for( int i = 0; i < n; i++ )
    {
        int tmp;
        cin >> tmp;
        arr.push_back( tmp );
    }

    Difference d( arr );
    d.computeDifference();
    cout << d.maxNum << endl;
    system( "pause" );
    return 0;
}