有一个长为n的数组A,求满足0≤a≤b<n的A[b]-A[a]的最大值。 给定数组A及它的大小n,请返回最大差值。

// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;

class LongestDistance {
public:
	int getDis(vector<int> A, int n) {
		int max = 0;
		for (int i = 0;i < n;i++)
		{

			for (int j = i + 1;j < n;j++)
			{
				if (max < (A[j] - A[i]))
				{
					max = A[j] - A[i];
				}
			}
		}
		return max;
	}
};
int main()
{
	LongestDistance ld;
	vector<int> A = { 10,5 };
	cout << ld.getDis(A,2) << endl;
	
	return 0;
};
posted @ 2017-02-25 21:07  wdan2016  阅读(1336)  评论(0编辑  收藏  举报