| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 499 人关注过本帖
标题:1003继续追问.....
只看楼主 加入收藏
Buger
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2013-3-20
结帖率:84.62%
收藏
已结贴  问题点数:20 回复次数:5 
1003继续追问.....
程序代码:
Max Sum

 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

 Total Submission(s): 98153    Accepted Submission(s): 22614

 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

 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 <stdio.h>
#define N 100000 + 10
int main() {
    int T, n, a[N], sum, t, i, k, j, b, sum1, c;
    scanf("%d", &T);
        for(t = 1; t <= T; t++) {
            scanf("%d", &n);
            sum = 0; b = 0;
            for(i = 1; i <= n; i++) {
                scanf("%d", &a[i]);
                if(b > 0) b += a[i];
                else b = a[i];
                if(b > sum) {
                    sum = b;
                    k = i;
                }
            }
            sum1 = 0; c = 0;
            for(i = k; i >= 1; i--) {
                if(c > 0) c += a[i];
                else c = a[i];
                if(c >= sum1) {
                    sum1 = c;
                    j = i;
              }
          }
          printf("Case %d:\n%d %d %d\n", t, sum, j, k);
          if(t < T) printf("\n");
      }
      return 0;

 }
上次听了B版的建议后,昨晚想了很久,经过一番修改但是还是WA,求解释
搜索更多相关主题的帖子: job max sequence Java 
2013-03-24 10:20
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
收藏
得分:20 
改了一下,你自己看
注意这句话:If there are more than one result, output the first one. Output a blank line between two cases.
程序代码:
#include <stdio.h>
#define N 100000 + 10
int main() {
    int T, n, a[N], sum, t, i, k, j, b, sum1, c;
    scanf("%d", &T);
        for(t = 1; t <= T; t++) {
            scanf("%d", &n);
            sum = -1001; b = 0;
            for(i = 1; i <= n; i++) {
                scanf("%d", &a[i]);
                if(b > 0) b += a[i];
                else b = a[i];
                if(b > sum) {
                    sum = b;
                    k = i;
                }
            }
            c = 0;
            for(i = k; i >= 1; i--) {
                c += a[i];
                if(c == sum)
                    j = i;
          }
          printf("Case %d:\n%d %d %d\n", t, sum, j, k);
          if(t < T) printf("\n");
      }
      return 0;
}
2013-03-24 12:09
Buger
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2013-3-20
收藏
得分:0 
谢谢,还是考虑欠缺,没有保证2个sum的和一样....20分全你的....
2013-03-24 12:39
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
收藏
得分:0 
 sum = -1001;

你没发现这个不同吗?
2013-03-24 12:49
Buger
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2013-3-20
收藏
得分:0 
还真没发现....  
2013-03-24 13:03
Buger
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2013-3-20
收藏
得分:0 
原来是这样...我明白了...免得第一个数输入的是-1000
2013-03-24 13:18
快速回复:1003继续追问.....
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018854 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved