Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

最大字段和问题

$
0
0

基本求和:

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int maxSubSum(int *a,int n){
	int max=0,b=0;
	for(int i=0;i<n;i++){
		if(b>0)
			b+=a[i];
		else
			b=a[i];
		if(b>max)
			max=b;
		}
	return max;
	}


void main(){

	int a[10];
	srand(time(NULL));
	for(int i=0;i<10;i++){
		a[i]=rand()%10+1-5;
		cout<<a[i]<<" ";
		}
	cout<<endl;

	cout<<"max sub sum: "<<maxSubSum(a,10)<<endl;
	
	

	cout<<endl;
	system("pause");
	}


作者:wodwl 发表于2013-3-9 16:57:43 原文链接
阅读:55 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>