zoukankan      html  css  js  c++  java
  • 最大连续子序列&&MAX SUM

    //错的莫名其妙的O w O

    第二个的格式也是莫名其妙的

    Input
    测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。

    Output
    对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
    素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。

    Sample Input
    6
    -2 11 -4 13 -5 -2
    10
    -10 1 2 3 4 -5 -23 3 7 -21
    6
    5 -8 3 2 5 0
    1
    10
    3
    -1 -5 -2
    3
    -1 0 -2
    0

    Sample Output
    20 11 13
    10 1 4
    10 3 5
    10 10 10
    0 -1 -2
    0 0 0

    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

     1 #include <stdio.h>
     2 #include <string.h>
     3 int main()
     4 {
     5       int first,last,temp,n,i,j,flag,thissum;
     6       int a[22222];
     7       while(scanf("%d",&n)&&n)
     8       {
     9           flag=0;
    10           thissum=0;
    11           memset(a,0,sizeof(a));
    12           for(i=1;i<=n;i++)
    13           {
    14                 scanf("%d",&a[i]);
    15                 if(a[i]>=0)
    16                     flag=1;
    17           }
    18                  first=last=temp=1;
    19           if(!flag)
    20           {
    21               printf("0 %d %d
    ",a[1],a[n]);
    22               continue;
    23           }
    24           int max=-33333;
    25           for(i=1;i<=n;i++)
    26           {
    27               thissum+=a[i];
    28                 if(thissum>max)
    29                 {
    30                     max=thissum;
    31                     first=temp;
    32                     last=i;
    33                 }
    34                 if(thissum<0)
    35                 {
    36                     thissum=0;
    37                     temp=i+1;
    38                 }
    39           }
    40           printf("%d %d %d
    ",max,a[first],a[last]);
    41       }
    42       return 0;
    43 }
    View Code
     
     
  • 相关阅读:
    SVN中Revert changes from this revision 跟Revert to this revision
    bootstrap中如何使input中的小图标获得点击事件
    基于Bootstrap使用jQuery实现输入框组input-group的添加与删除-改进版
    MVC4 Controller 与 WebApi 的 Session 传值问
    MVC Razor与javascript混编(js中嵌入razor)
    Asp.net mvc怎么在razor里写js代码
    MVC后台数据赋值给前端JS对象
    ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)
    ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
    【记录】【solr】solr7.2.1原子更新
  • 原文地址:https://www.cnblogs.com/awsent/p/4268555.html
Copyright © 2011-2022 走看看