Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148620
  • 博文数量: 33
  • 博客积分: 1494
  • 博客等级: 上尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-24 21:50
文章分类

全部博文(33)

文章存档

2014年(1)

2013年(1)

2012年(2)

2011年(3)

2010年(26)

我的朋友

分类: C/C++

2010-10-22 13:42:32

#include
using namespace std;
/****************************************************************************************/
/*第一种方法,容易想到,时间复杂度为O(n*n) **************************************/
/*传入一数组首地址和数组元素个数,传出最大子序列和,并同时打印输出下标范围*/
/****************************************************************************************/
int Max1(int *data, int n)
{
    int i,j,left = 0, right = 0;
    int result = 0;
    int max = data[0];
    for (i = 0; i < n; i ++)
    {
        result = 0;
        for (j = i; j < n; j ++)
        {
            result += data[j];
            if (result > max)
            {
                max = result;
                left = i;
                right = j;
            }
            
        }
    }
    cout<<"left = "<    cout<<"The largest result is :"<
    cout<<"Second way:"<    cout<<"The largest result is :"<    return 0;
}
运行结果如下:


阅读(794) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-25 16:29:42

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com