最大连续子序列&&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\n",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\n",max,a[first],a[last]);
41       }
42       return 0;
43 }
View Code
 
 
posted @ 2015-02-02 20:03  江豚  阅读(175)  评论(0编辑  收藏  举报