最大连续元素之和

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. 

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000). 

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases. 

Sample Input

 2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5 

Sample Output

 Case 1:
14 1 4

Case 2:
7 1 6
#include<iostream>

#include<cstdio>

using namespace std;

int main()

{

    int n,t,i,c;

    int a[100002];

    scanf("%d",&t);     //数据的组数

    for(c=1;c<=t;c++)   //用c来记录数据的组数和循环次数,还有控制换行的输出

    {

        int k=1,st=0,en=0,summax=-1000,sum=0;

        scanf("%d",&n);

        for(i=0;i<n;i++)  //输入数据

        {

            scanf("%d",&a[i]);

        }

        for(i=0;i<n;i++)  //此循环控制连续元素相加,sum与summax的比较,sum<0的情况

        {

            sum+=a[i];  //连续元素一个个加起来

            if(sum>summax) //如果sum>summax,把sum赋值给summax(把最大的连续元素之和用summax来存)

            {

                summax=sum;

                st=k; //记录头位置

                en=i+1;  //记录尾位置

            }

            if(sum<0) //若sum<0,则sum=0,且sum<0的这一连续元素不要了,从下一个元素开始

            {

                sum=0;

                k=i+2;  //从下一个元素开始,k是记录头位置,k=i+2即从尾位置的下一个元素开始算

            }

        }

        printf("Case %d:\n%d %d %d\n",c,summax,st,en);

        if(c!=t) //换行的控制      cout<<endl;  

    }

    return 0;

}

原文地址:https://www.cnblogs.com/liufei-/p/9295044.html

时间: 2024-08-12 14:24:10

最大连续元素之和的相关文章

最大连续子序列之和,最大连续子序列乘积

最大连续子序列之和问题描述为:数组中里有正数也有负数,连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值.分析,对数组a进行一遍扫描,sum[i] 为前i个元素中,包含第i个元素且和最大的连续子数组,MaxSum保存当前子数组中最大和,对于a[i+1]来说,sum[i+1] = sum[i]+a[i+1],此时如果sum[i+1]<0,那么sum需要重新赋0,从i+1之后开始累加,如果sum[i+1]>0,那么MaxSum = max(MaxSum, Sum[i

求一个n阶方阵对角线元素之和

求一个n阶方阵对角线元素之和? public class shuixian { public static void main(String[] args) {  // TODO 自动生成的方法存根  int sum=0;  Scanner reader=new Scanner(System.in);  System.out.println("请输入方阵的阶数");  int n=reader.nextInt();  int[][] a=new int[n][n];  for(int

c编程:求出4&amp;#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

【CUDA并行编程之七】数组元素之和

现在需要求得一个数组的所有元素之和,之前感觉似乎不太可能,因为每个线程只处理一个元素,无法将所有元素联系起来,但是最近学习了一段代码可以实现,同时也对shared memory有了进一步的理解. 一.C++串行实现 串行实现的方法非常之简单,只要将所有元素依次相加就能够得到相应的结果,实际上我们注重的不是结果,而是运行的效率.那么代码如下: array_sum.cc: #include<iostream> #include<stdio.h> #include "kmean

用C随机产生的正整数存到数组中,并求数组中的所有元素最大值、最小值、平均值以及各元素之和,及第二大值。

用 C 求一组随机数的第二大值,不能通过对整体排序求得 1 随机产生20个[10 , 50]的正整数存到数组中,并求数组中的所有元素最大值.最小值.平均值以及各元素之和,及第二大值.        int a[20];    int sum = 0; //存储数组元素的和    //为数组赋值    printf("数组中的元素为:\n ");    for (int i = 0; i < 20; i ++) {        a[i] = arc4random() % 41 +

UVa 1210 连续素数之和

https://vjudge.net/problem/UVA-1210 题意: 输入整数n,有多少种方案可以把n写成若干个连续素数之和? 思路: 先素数打表,然后求个前缀和. 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 typedef long lon

生成一个n*n的方阵,然后输出此方阵对角线上元素之和

//输入一个正整数n(1<n<10),根据以下公式生成一个n*n的方阵,然后输出此方阵对角线上元素之和 //公式为a[i][j]=i*n+j+1 (0=<i<n,0=<j<n) 源代码: #include<stdio.h> #include<stdlib.h> #define N 10 //输入一个正整数n(1<n<10),根据以下公式生成一个n*n的方阵,然后输出此方阵对角线上元素之和 //公式为a[i][j]=i*n+j+1 (0

c编程:求出4&#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

求两条对角线上行、列下标均为偶数的各元素之和。

//求两条对角线上行.列下标均为偶数的各元素之和. #include <stdio.h> int main() { int sum=0; int i,j; int a[5][5]; //为数组赋值 for(i=0;i<5;i++) { for(j=0;j<5;j++) { scanf("%d",&a[i][j]); } } //求两条对角线上行.列下标均为偶数的各元素之和. for(i=0;i<5;i++) { for(j=0;j<5;j++