题目:

输入一组整数,求出这组数字子序列和中最大值。也就是只要求出最大子序列的和,不必求出最大的那个序列。例如:

序列:-2 11 -4 13 -5 -2,则最大子序列和为20。

序列:-6 2 4 -7 5 3 2 -1 6 -9 10 -2,则最大子序列和为16。

1.

 /*
算法一:穷举法(3个for)
时间复杂度:O(n^3) */
#include <stdio.h>
#include <malloc.h> int Max = ;
int find_max(int len, int arr[]){
int i, j, k, sum;
for(i=; i<len; i++){
for(j=i; j<len; j++){
sum = ;
for(k=i; k<=j; k++){
sum += arr[k];
}
if(sum > Max){
Max = sum;
}
}
}
return Max; } int main(){
int i, len, *arr;
printf("请输入数组的长度: ");
scanf("%d",&len);
arr = (int *)malloc(sizeof(int)*len);
printf("请输入数组的值:");
for(i=; i<len; i++){
scanf("%d",&arr[i]);
}
find_max(len,arr);
printf("最大连续子序列和 :%d\n", Max ); return ;
}

2.

 /*
算法二:算法一的优化 (2个for)
时间复杂度:O(n^2)
*/
#include <stdio.h>
#include <malloc.h> int Max = ;
int find_max(int arr[],int n, int len){
int i, sum = ;
for(i=n; i<len; i++){
sum += arr[i];
if(sum > Max){
Max = sum ;
}
}
return Max;
} int main(){
int i, len, *arr;
printf("请输入数组的长度:");
scanf("%d",&len);
arr = (int *)malloc(sizeof(int)*len);
printf("请输入数组的值:");
for (i=; i<len; i++)
{
scanf("%d", &arr[i]);
} for(i=; i<; i++){
find_max(arr,i, len);
} printf("最大连续子序列和:%d \n",Max); return ;
}

 将代码进行以下修改,可以得到该最大子序列和的开始元素和结束元素(low,high)

 int find_max(int len, int arr[]){
int i, j, sum, low, high;
for(i=; i<len; i++){
sum = ;
for(j=i; j<len; j++){
sum += arr[j];
if(sum > Max){
Max = sum;
low = i;
high = j;
}
}
}
printf("The low is :%d\nThe high is : %d\n",arr[low],arr[high]);
return Max;
}

3.

 /*
算法三:联机算法
时间复杂度:O(n)
*/
#include <stdio.h>
#include <malloc.h> int Max = ;
int find_max(int len, int arr[])
{
int i, sum = ;
for(i=; i<len; i++)
{
sum += arr[i];
if(sum > Max)
{
Max = sum;
}else if(sum < ){
sum = ;
}
} return Max;
} int main(){
int i, len, *arr;
printf("请输入数组的长度:");
scanf("%d",&len);
arr = (int *)malloc(sizeof(int)*len);
printf("请输入数组的值:");
for (i=; i<len; i++)
{
scanf("%d", &arr[i]);
}
find_max(len, arr);
printf("最大连续子序列和:%d \n",Max);
return ;
}

对以上代码进行小的改动,通过生成一系列的随机数进行测试程序运行的时间,由于程序运行的很快,需要通过重复循环取平均值的方法得到程序执行一次的时间。参考:http://www.cnblogs.com/LinSL/p/7475001.html

 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h> clock_t start,stop;
double duration; void fun(int len,int arr[]); int main(){
int len, *arr, i;
printf("Please enter the len:");
scanf("%d",&len);
arr = (int*)malloc(sizeof(int)*len); for(i=; i<len; i++){
arr[i] = rand()%-;/*产生-1000~1000之间的随机数*/
printf("%d ",arr[i]);
}
printf("\n");
start = clock();
fun(len,arr);
stop = clock();
duration = ((double)(stop - start))/CLK_TCK;
printf("The duration is:%f\n",duration);/*0.0000*/
return ;
} void fun(int len,int arr[]){
int max=,
sum=,
i;
for(i=; i<len; i++){
sum += arr[i];
if(sum > max){
max = sum;
}else if(sum < ){
sum = ;
}
}
printf("The max_sum is:%d\n",max);
}

参考:http://blog.163.com/kevinlee_2010/blog/static/169820820201010495438247/

C_求最大连续子序列和的更多相关文章

  1. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  2. HDU 1231:最大连续子序列 解题报告

    第一次写博客, 自己总结写出了一道题感觉值得保存. 自己总结的规律:求最大连续子序列, 可以先求包括第N项在内的前N项最大值, (因为每一项都求过后, 前N项最大值最大的那一个元素所连续的序列即为最大 ...

  3. dp经典问题-最大连续子序列和 hdu1003

    题目描述: 这道题我先后做过三遍,结果每一遍都没有做出来.今天再仔仔细细的研究了一下,才发现用动态规划更好理解. 关于求最大连续子序列和的博文转载如下:https://www.cnblogs.com/ ...

  4. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值

    E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Educational Codeforces Round 63 (Rated for Div. 2) D dp(最大连续子序列)

    https://codeforces.com/contest/1155/problem/D 题意 一个n个数的数组\(a[i]\),可以选择连续的一段乘x,求最大连续子序列的值 题解 错误思路:贪心, ...

  6. 《github一天一道算法题》:分治法求数组最大连续子序列和

    看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...

  7. K:求取数组中最大连续子序列和的四个算法

    相关介绍:  求取数组中最大连续子序列和问题,是一个较为"古老"的一个问题.该问题的描述为,给定一个整型数组(当然浮点型也是可以的啦),求取其下标连续的子序列,且其和为该数组的所有 ...

  8. 最大连续子序列乘积(DP)

    题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...

  9. [ACM_其他] 总和不小于S的连续子序列的长度的最小值——尺缩法

    Description: 给定长度为n的整数数列,A[0],A[1],A[2]….A[n-1]以及整数S,求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. Input: 输入数据有 ...

随机推荐

  1. java.lang.OutOfMemoryError: unable to create new native thread

    ps -o nlwp 70753 sudo -u tomcat jmap -dump:format=b,file=fundmarketmanage.hprof 78894

  2. ASP.NET Core Http请求的处理流程

  3. [转] react-router4 + webpack Code Splitting

    项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...

  4. 关于mac远程链接window服务器以及实现共享文件

    要最近换了新电脑mac 虽然运行速度666  但是真的很多地方都使用不习惯 这里记录一下 关于 远程链接window主机的问题 方便以后用 首先是 链接: 在应用里 找到    然后类似于  wind ...

  5. mysql实现简单的增删改查,放入xmapp自带数据库中

    1.mysql概念:SQL-Structured Query Language,是一种特殊的语言,专用于操作关系型数据库服务器中的数据,所有的SQL语句分为四类: (1)DDL(2)DQL(3)DML ...

  6. js中的new Option默认选中

    new Option("文本","值",true,true).后面两个true分别表示默认被选中和有效! //js默认选中 var sel = document ...

  7. solr配置IKAnalyzer抛出ClassNotFoundException

    这个问题搞了很久,在QQ群上问了很久,关键很气人的是我居然被群主给开了.我也是醉了.我不知道我哪里得罪了那个solr群的群主. 废话不多说.抛出的异常如下: 刚开始一直认为是没有找到类,也就相当于没找 ...

  8. muduo学习笔记(六) 多线程的TcpServer

    目录 前言 多线程TcpServer EventLoopThreadPool 线程池设计模式 muduo中的使用 连接的建立.消息.销毁 on_connection on_message on_clo ...

  9. 二叉搜索树的java实现

    转载请注明出处 一.概念 二叉搜索树也成二叉排序树,它有这么一个特点,某个节点,若其有两个子节点,则一定满足,左子节点值一定小于该节点值,右子节点值一定大于该节点值,对于非基本类型的比较,可以实现Co ...

  10. python数据结构之插入排序

    插入排序(英语:Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,在从后向前扫描 ...