注册 登录
编程论坛 C++教室

杭电acm1003的题,出现Runtime Error (ACCESS_VIOLATION)该怎么改

玫瑰、 发布于 2016-03-10 21:22, 3881 次点击
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;
}
5 回复
#2
rjsp2016-03-11 09:34
int a[20]; 中的 20 是怎么来的?题目中原文是“N(1<=N<=100000)”
#3
玫瑰、2016-03-11 19:14
恩,理解错啦那会,我把输入的数弄成那个啦,我试试
#4
玫瑰、2016-03-11 19:18
答案还错。。。。。。。。

#5
rjsp2016-03-14 08:44
那你把题目中的测试用例测试过了吗?如果这一步你都不肯做的话,我就无话可说了。测试用例中有“Case 1”等,你的代码中有吗?

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

因为你不肯贴出题目要求(只有Input/Output),所以我猜题目要求是求最大子序列和,但看你的代码似乎求的是从左开始的子序列和
#6
玫瑰、2016-03-16 19:37
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]表示,这个是知道的。


1