杭电acm 1003题,为什么总是提示Runtime Error(ACCESS_VIOLATION),我测试结果没问题啊!求大神指教!
#include<stdio.h>#include<iostream>usingnamespacestd;typedefstruct{intmaxSum;intstart...
#include <stdio.h>#include <iostream>
using namespace std;
typedef struct
{
int maxSum;
int startIndex;
int endIndex;
}RESULT;
int Func(int array[], int n, RESULT *Result);
int main()
{
int n, i=0, k=0, j=0;
cin >> n;
int **pp = new int *[n];
RESULT *Result = new RESULT[n];
RESULT tmpRest;
for (i=0; i<n; i++)
{
cin >> k;
pp[i] = new int[k];
pp[i][0] = k;
for (j=1; j<k+1; j++)
{
cin >> pp[i][j];
}
}
//输出
for (int i=0; i<n; i++)
{
Func(pp[i], pp[i][0]+1, &tmpRest);
cout << "Case " << i+1 << ":" <<endl;
cout << tmpRest.maxSum <<" "<< tmpRest.startIndex <<" "<< tmpRest.endIndex << endl;
cout << endl;
}
free(pp); free(Result);
return 0;}
int Func(int array[], int n, RESULT *Result)
{
int i=0, j=0, k=0;
Result->maxSum =array[1];
Result->startIndex = 1;
Result->endIndex = 1;
RESULT tmpRest;
for (i=1; i<n; i++)
{
tmpRest.maxSum = 0;
tmpRest.startIndex = i;
tmpRest.endIndex = i;
for(j=i; j<n; j++)
{
tmpRest.maxSum = tmpRest.maxSum + array[j];
if (tmpRest.maxSum > Result->maxSum)
{
Result->maxSum = tmpRest.maxSum;
Result->endIndex = j;
}
if(j<n-1)
{
if (array[j] < 0)
j++;
}
}
}
return 0;
} 展开
using namespace std;
typedef struct
{
int maxSum;
int startIndex;
int endIndex;
}RESULT;
int Func(int array[], int n, RESULT *Result);
int main()
{
int n, i=0, k=0, j=0;
cin >> n;
int **pp = new int *[n];
RESULT *Result = new RESULT[n];
RESULT tmpRest;
for (i=0; i<n; i++)
{
cin >> k;
pp[i] = new int[k];
pp[i][0] = k;
for (j=1; j<k+1; j++)
{
cin >> pp[i][j];
}
}
//输出
for (int i=0; i<n; i++)
{
Func(pp[i], pp[i][0]+1, &tmpRest);
cout << "Case " << i+1 << ":" <<endl;
cout << tmpRest.maxSum <<" "<< tmpRest.startIndex <<" "<< tmpRest.endIndex << endl;
cout << endl;
}
free(pp); free(Result);
return 0;}
int Func(int array[], int n, RESULT *Result)
{
int i=0, j=0, k=0;
Result->maxSum =array[1];
Result->startIndex = 1;
Result->endIndex = 1;
RESULT tmpRest;
for (i=1; i<n; i++)
{
tmpRest.maxSum = 0;
tmpRest.startIndex = i;
tmpRest.endIndex = i;
for(j=i; j<n; j++)
{
tmpRest.maxSum = tmpRest.maxSum + array[j];
if (tmpRest.maxSum > Result->maxSum)
{
Result->maxSum = tmpRest.maxSum;
Result->endIndex = j;
}
if(j<n-1)
{
if (array[j] < 0)
j++;
}
}
}
return 0;
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询