设为首页 加入收藏

TOP

数对之差的最大值
2014-11-23 21:27:56 来源: 作者: 【 】 浏览:7
Tags:最大值
题目:在数组中,数字减去它右边的数字得到一个数对之差。求所有数对之差的最大值。例如在数组{2, 4, 1, 16, 7, 5, 11, 9}中,数对之差的最大值是11,是16减去5的结果。
#include
using namespace std;

void main()
{
	int data[]={2, 4, 1, 16, 7, 5, 11, 9};
	int length=sizeof(data)/sizeof(int);

	int i;
	int compare=data[length-1];
	int max=0;
	
	for(i=length-1;i>=0;i--)
	{
		int temp_sub;
		if(data[i]>compare)
		{
			temp_sub=data[i]-compare;
			if(temp_sub>max)
			{
				max=temp_sub;
			}
		}
		else
		{
			compare=data[i];
		}
	}
	cout< 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 2569 下一篇POJ 2377 Bad Cowtractors

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)