博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ_ACM_Max Sum
阅读量:6265 次
发布时间:2019-06-22

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

Problem Description
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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1:14 1 4Case 2:7 1 6
 
Code
two programs are all accepted.
View Code
1 //input, then two for, record begin, end and the max 2 #include 
3 #define N 100000 4 int f[N + 5]; 5 int main() 6 { 7 int T, n, i, j, k, t, begin, end, max, sum; 8 scanf("%d", &T); 9 for (k = 1; k <= T; k++)10 {11 //input12 scanf("%d", &n);13 for (t = 0; t < n; t++)14 scanf("%d", &f[t]);15 //find the result16 for (max = -1001, begin = 0, i = 0; i < n; i++)17 {18 for (sum = 0, j = i; j < n; j++)19 {20 sum += f[j];21 //if sum is bigger than max, then record.22 if (sum > max)23 {24 begin = i;25 end = j;26 max = sum;27 }28 //if sum is smaller than zero, then jump to j. ★29 if (sum < 0)30 {31 i = j;32 break;33 }34 }35 }36 //formatting printing37 printf("Case %d:\n", k);38 printf("%d %d %d\n", max, begin + 1, end + 1);39 if (k != T)40 puts("");41 }42 return 0;43 }
View Code
1 //input, then two for, record begin, end and the max 2 #include 
3 #define N 100000 4 int f[N + 5]; 5 int main() 6 { 7 int T, n, i, j, k, t, begin, end, max, sum, flag; 8 scanf("%d", &T); 9 for (k = 1; k <= T; k++)10 {11 flag = 0;12 //input13 scanf("%d", &n);14 for (t = 0; t < n; t++)15 {16 scanf("%d", &f[t]);17 if (f[t] > 0)18 flag = 1;19 }20 max = -1001;21 begin = 0;22 //if flag is equal to zero which means the all number is small than zero23 if (flag == 0)24 {25 for (i = 0; i < n; i++)26 {27 if (f[i] > max)28 {29 max = f[i];30 begin = i;31 end = i;32 }33 }34 }35 else 36 {37 for (i = 0; i < n; i++)38 {39 sum = 0;40 //if the begin number is small than zero, then jump.41 if (f[i] < 0)42 continue;43 for (j = i; j < n; j++)44 {45 sum += f[j];46 //if sum is small than zero, then jump.47 if (sum < 0)48 break;49 if (sum > max)50 {51 begin = i;52 end = j;53 max = sum;54 }55 }56 }57 }58 printf("Case %d:\n", k);59 printf("%d %d %d\n", max, begin + 1, end + 1);60 if (k != T)61 puts("");62 }63 return 0;64 }

The first one

for example, 4 4 -5 1 2. when i = 1, j = 2, then sum = 4 + -5 = -1, then make i = j = 2. Because the sum of the before number is samller than zero, you can start with postive interger.

The second one

the program judge whether all number is  negative, when it is negative, just find the max number and the position. Besides, when it begin with negative number, you can jump.

Note the two programs is different.

 

转载于:https://www.cnblogs.com/chuanlong/archive/2012/11/12/2767137.html

你可能感兴趣的文章
int类型究竟占几个字节
查看>>
13.使用toggle()方法绑定多个函数
查看>>
springboot集成redis
查看>>
装饰器的应用-装饰器带参数和不带参数
查看>>
数据库 --> SQL 和 NoSQL 的区别
查看>>
USB学习笔记连载(二十):FX2LP如何实现高速和全速切换(转载)
查看>>
ubuntu下搭建JAVA开发环境【转】
查看>>
和菜鸟一起学c之gcc编译过程及其常用编译选项【转】
查看>>
Linux内核驱动--硬件访问I/O【原创】
查看>>
使用NSUserDefaults保存自定义对象(转)
查看>>
linux上查看swf文件.靠谱
查看>>
sql server两种分页方法
查看>>
一本离线的百科全书,当然无法和一本在线的百科全书抗衡。所谓的常识,在你的思考中被重构,根源就在于在线的崛起。...
查看>>
Floyd算法
查看>>
CentOS 6.4下安装Oracle 11gR2
查看>>
linux建立用户 详细
查看>>
jquery获取radio的值
查看>>
创建索引
查看>>
jQuery基础-创建HTML
查看>>
spring boot 热部署
查看>>