您的位置:首页 > 其它

求出数组中相邻三个数之和最大的~

2010-03-24 09:49 232 查看
// 求出数组中相邻三个数之和最大的一组
#include "stdafx.h"
#include <stdio.h>     
#include <math.h>  
#include <iostream>
#include <String>
using namespace std;
int main()
{
	// 输入数组的最大值
	int array_size = 100;
	cout<<"please input the array_size of the array : ";
	cin>>array_size;
	int num[10000];
	for(int i = 0; i < array_size; i++)
	{
		num[i] = rand() % (array_size + 1);
		cout<< i <<" the random number is :"<<num[i]<<endl;
	}
	// 保存最大的值
	int max_num = 0;
	int max_index = 0;
	for(int i=0; i<array_size - 2; i++)
	{
		int temp_max = 0;
		for(int j=i; j<i+3; j++)		
		{
			temp_max += num[j];
		}
		if(temp_max > max_num)
		{
			max_num = temp_max;
			max_index = i;
		}
	}
	
	cout<<"the max num of the three in the array is :"<<max_num<<endl;
	cout<<"the max num index of the array is : "<<max_index<<endl;
	system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: