博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最大连续子序列&&MAX SUM
阅读量:4609 次
发布时间:2019-06-09

本文共 2286 字,大约阅读时间需要 7 分钟。

//错的莫名其妙的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 
2 #include
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
 
 

转载于:https://www.cnblogs.com/awsent/p/4268555.html

你可能感兴趣的文章
良好的编程规范
查看>>
struts2 入门
查看>>
.net 编译原理
查看>>
mean 快速开发和现有技术的对比分析
查看>>
Metro Style app :浏览器扩展
查看>>
linux的kernel是怎样工作的(TI_DM36X_ARM系统)(1)
查看>>
[luogu4310] 绝世好题 (递推)
查看>>
[luogu3203 HNOI2010] 弹飞绵羊 (分块)
查看>>
-Dmaven.multiModuleProjectDirectory system propery is not set.
查看>>
Python2 unichr() 函数
查看>>
Python 字典 copy()方法
查看>>
Minimum Path Sum
查看>>
Remove Duplicates from Sorted Array II
查看>>
常量指针和指针常量巧妙记忆方法[转]
查看>>
python-haproxy作业讲解视频总结
查看>>
mui搜索框 搜索点击事件
查看>>
select2 下拉搜索控件
查看>>
WebAPI常见的鉴权方法,及其适用范围
查看>>
08. 删除重复&海量数据
查看>>
重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++
查看>>