| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3630 人关注过本帖
标题:杭电acm1003的题,出现Runtime Error (ACCESS_VIOLATION)该怎么改
只看楼主 加入收藏
玫瑰、
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-7-24
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:5 
杭电acm1003的题,出现Runtime Error (ACCESS_VIOLATION)该怎么改
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 <iostream>
using namespace std;
int main()
{
    int n,i,k,j,start,finish;
    double sum,sum1,max=0;
    int a[20];
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>k;
        sum=sum1=0;
        for(j=1;j<=k;j++)
        {
            cin>>a[j];
        }
        for(j=1;j<=k;j++)
        {
            sum1=sum;
            start=finish=1;
            sum=sum+a[j];
            if(sum>sum1)
            {
                max=sum;
                finish=j;
            }
            else{finish=j-1;}
        }
        cout<<max<<" "<<start<<" "<<finish<<endl;
        cout<<endl;
    }
    return 0;
}
2016-03-10 21:22
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
int a[20]; 中的 20 是怎么来的?题目中原文是“N(1<=N<=100000)”
2016-03-11 09:34
玫瑰、
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-7-24
收藏
得分:0 
恩,理解错啦那会,我把输入的数弄成那个啦,我试试
2016-03-11 19:14
玫瑰、
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-7-24
收藏
得分:0 
答案还错。。。。。。。。

2016-03-11 19:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
那你把题目中的测试用例测试过了吗?如果这一步你都不肯做的话,我就无话可说了。测试用例中有“Case 1”等,你的代码中有吗?

你的代码中,数组下标是从1开始的,我不知道你这属于特意的优化,还是根本不会C/C++。因为我不能确定,所以也不说你错

因为你不肯贴出题目要求(只有Input/Output),所以我猜题目要求是求最大子序列和,但看你的代码似乎求的是从左开始的子序列和
2016-03-14 08:44
玫瑰、
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-7-24
收藏
得分:0 
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.
这是题,那个"case 1"是忽略了,for从1开始到<=n结束和从0开始到<n结束一样吧,数组a的第一个数用a[0]表示,这个是知道的。


2016-03-16 19:37
快速回复:杭电acm1003的题,出现Runtime Error (ACCESS_VIOLATION)该怎么改
数据加载中...
 
   



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

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