软件工程 最大连续序列和

今天软件工程课的时候,老师举个例子,最大的连续子序列的问题,自己在在脑中,想起来了在ACM中做过的题,

简要说一下思路:动态规划,找到状态转移方程是关键。定义两个数组a[],b[],一个存自己输入的数组,一个用来存连续和的值。状态转移方程:b[i]=max{b[i-1]+a[i],a[i]},找出最大的b[i]。

伪代码:

for(循环)

if(a[i]>0),

b[i]=b[i-1]+a[i],

else

b[i]=a[i]

我选择用的是选择排序,比较一轮就知道最大是哪一个,

max=b[0]

if(max<b[i])

max=b[i]

输入测试数据组数:test;再输入:数组的大小;再输入:数组的各个元素。

输出: 最大连续序列和

源代码:

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 using namespace std;
 5
 6 #define Max 1005
 7 int a[Max],b[Max];
 8
 9 int main()
10 {
11     int i,n,test,max=0;
12     scanf("%d",&test);
13     while(test--)
14     {
15         scanf("%d",&n);
16         for(i=0;i<n;i++)
17              scanf("%d",&a[i]);
18         b[1]=a[1];
19         for(i=2;i<=n;i++)
20         {
21             if(a[i]>0)
22                 b[i]=b[i-1]+a[i];
23             else
24                 b[i]=a[i];
25         }
26         max=b[1];
27         for(i=2;i<=n;i++)
28         {
29              if(max<b[i])
30                 max=b[i];
31
32         }
33         printf("%d",max);
34     }
35     return 0;
36 }

然后再找到最大连续序列和的同时,还需要输出从第几个数到第几个数的脚标。

题目在hdu oj:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3&sectionid=2&problemid=1

就是输出变态一点,其实思路掌握了,就行。

我的代码:

 1 #include<iostream>
 2 #define N 100010
 3 using namespace std;
 4 int a[N],d[N];
 5 int main()
 6 {
 7     int test,n,i,max,k,f,e;
 8     cin>>test;
 9     k=1;
10     while(test--)
11     {
12         cin>>n;
13         for(i=1;i<=n;i++)
14             cin>>a[i];
15         d[1]=a[1];
16         for(i=2;i<=n;i++)
17         {
18             if(d[i-1]<0) d[i]=a[i];
19             else d[i]=d[i-1]+a[i];
20         }
21         max=d[1];e=1;
22         for(i=2;i<=n;i++)
23         {
24             if(max<d[i])
25             {
26                 max=d[i];e=i;
27             }
28         }
29         int t=0;
30         f=e;
31         for(i=e;i>0;i--)
32         {
33             t=t+a[i];
34             if(t==max)    f=i;
35         }
36         cout<<"Case "<<k++<<":"<<endl<<max<<" "<<f<<" "<<e<<endl;
37         if(test) cout<<endl;
38     }
39     return 0;
40 }

时间: 2024-08-11 09:57:47

软件工程 最大连续序列和的相关文章

算法题:找出一个数组中相加值最大的连续序列元素

package arithmetic; /** * @author SHI * 求一个数组中相加值最大的连续序列元素 */ public class MaxSequence { public static void main(String[] args) { int[] a=new int[]{-2,9,-3,4,-6,7,-6,4}; findBigSequence(a); } /** * 思想: (1)计算出该数组的所有元素和,假设该值为最大 * (2)从数组下标1到a.length-1依次

lintcode 最长上升连续子序列 II(二维最长上升连续序列)

题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 II 给定一个整数矩阵(其中,有 n 行, m 列),请找出矩阵中的最长上升连续子序列.(最长上升连续子序列可从任意行或任意列开始,向上/下/左/右任意方向移动). 样例 给定一个矩阵 [ [1 ,2 ,3 ,4 ,5], [16,17,24,23,6], [15,18,25,22,7], [14,1

[LeetCode] Longest Consecutive Sequence 求最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

求最大连续序列和的一个巧妙方法,时间复杂度O(n)

1 # include<iostream> 2 using namespace std; 3 long long a[1000000]; 4 long long b[1000000]; //b[i]表示数列前i+1个数的最大连续序列和 5 int main(){ 6 long long n=0,i=0; 7 while(cin>>n){ 8 for(i=0;i<n;i++) 9 cin>>a[i]; 10 b[0]=a[0]; 11 long long ma=a[

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

【6】连续序列和为s

题目:输入一个整数s,打印出全部和为s的连续整数序列(至少含有2个数).比如输入9,则输出2.3.4和4.5两个序列 方案一:因为序列至少要2个数,则两个数上限值为(1+s)/2,我们能够枚举该序列的起点和终点求全部满足的序列.时间复杂度为O(n^2),效率比較低 方案二:我们设置两个指针start和end分别表示当前序列的起点和终点,并记序列和为sum.当sum = s的时候输出这个序列,而且end往后移动一位:假设sum > s,则start往后移动一位:假设sum < s,则end要往后

连续序列和为s

题目:输入一个整数s,打印出所有和为s的连续整数序列(至少含有2个数).例如输入9,则输出2.3.4和4.5两个序列 方案一:由于序列至少要2个数,则两个数上限值为(1+s)/2,我们可以枚举该序列的起点和终点求所有满足的序列.时间复杂度为O(n^2),效率比较低 方案二:我们设置两个指针start和end分别表示当前序列的起点和终点,并记序列和为sum.当sum = s的时候输出这个序列,并且end往后移动一位:如果sum > s,则start往后移动一位:如果sum < s,则end要往后

[Leetcode] Longest consecutive sequence 最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4. Your algorithm should run in O(n

GEEK编程练习— —最长连续序列

题目 给定一个无序的整数数组,返回最长连续序列的长度.要求时间复杂度为O(n). 输入 [100, 4, 200, 1, 3, 2, 0, -1] 输出 6 分析 因为要求时间负责度为O(n),所以不能先排序再查找.所以想到查询最快的hash表,记录每个元素是否使用,对每个元素,往左右扩张,直到不连续为止. 代码 #include <iostream> #include <unordered_map> #include <algorithm> using namespa