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 LEN 5000
int a[LEN];
int main (void)
{
int n,i,j,sum,p,q,max,m,x,y;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
max=a[0];
scanf("%d",&m);
for(j=0;j<m;j++)
scanf("%d",&a[j]);
for(p=0;p<m;p++)
{
sum=0;
for(q=p;q<m;q++)
{
sum+=a[q];
if(sum>max)
{
max=sum;
x=p,y=q;
}
}
}
printf("Case %d:\n%d %d %d\n",i+1,max,x+1,y+1);
printf("\n");
}
}
return 0;
}
我开了很大的数组还是不行的啊?哪里不对?
mm是什么? 我sum不是有初值0了啊?循环结束条件题目没要求 的啊?
我试了没用啊,直接time limited
追答TLE 是你的算法有问题,你两重for 合起来复杂度是 O(n^2 ) n=100000平方是多少你自己算一下,肯定要超时的
本回答被提问者采纳杭电acm1003为什么都过不了,显示Runtime Error(ACCESS_VIOLATION)
你的 mm 没有赋初值,所以会Runtime Error 还有就是 while的循环结束条件不足,如果到最后 m还是没有==0你就错了 这是ac的代码 include
...为什么总是提示Runtime Error(ACCESS_VIOLATION),我测试结果没问题啊...
Runtime Error一般是数组越界, 可能你数组定义小了
杭电acm总是Runtime Error (ACCESS_VIOLATION)
runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。比如说:除以零 数组越界:int a[3]; a[10000000]=10 指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10 使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *...
杭电OJ1004题,结果Runtime Error(ACCESS_VIOLATION),请大牛为我查错
另外还有一个问题, 你忘记初始化q了,你仅仅只是给q赋了一个全局的初值0,每个case开始的时候都应该初始化。 在while(N){的第一句写q=0;这样就可以了
杭电acm 一直显示Runtime Error (ACCESS_VIOLATION) http:\/\/acm.hdu...
数组开小了。。你的代码这样改就AC了。。include<stdio.h> include<string.h> int main(){ int n;char zifu[1000];int i;scanf("%d",&n);getchar();for(i=0;i<n;i++){ int j,count=0;gets(zifu);for(j=0;j<strlen(zifu);j++){ if(zifu[j]<0)count++;} printf("%d\\n"...
...杭电ACM上就显示runtime,error ACCESS_VIOLATION,求大神讲解_百度知 ...
char string_color[1000][15];char *string_num_max1[1000];可能是堆栈大小的问题,你这些变量都是在堆栈里分配的,换成动态分配试试.
杭电acm2023,Runtime Error (ACCESS_VIOLATION)?
即 a[51][6] \/\/ 另外一个问题就是 每次循环后count1 没有重新置零 \/\/***#include <stdio.h>int main(){ int n, m, i, j, count1, count2; double a[51][6]; \/\/后面程序访问到 n 所以应该定义为a[51][6] while(scanf("%d%d", &n, &m) != EOF) {...
杭电ACM1004,Runtime Error,(ACCESS_VIOLATION)
include<cstring> using namespace std;struct node { int count;char color[15];}*c;int main(){ int n,i,j,max=1;char pop[15];while(cin>>n,n){ c=(struct node*)malloc(sizeof(c[0])*n);\/\/开辟n个结构体数组 cin>>c[0].color;c[0].count=1;strcpy(pop,c[0].color);...
...1002为什么总是 Runtime Error(ACCESS_VIOLATION)?
include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>void addbig(char *a, char *b){ int ret; int c = 0; int lena = strlen(a); int lenb = strlen(b); int maxlen = (lena > lenb ? lena : lenb) + 1; int len = maxlen; ...
杭电1004结果一直是Runtime Error(ACCESS_VIOLATION)
include <string> using namespace std;int main(){ string s, color[1000]; \/\/ 1000 int t, i, j, k, max, mid, num[1000]; \/\/ 1000 while(cin>>t && t != 0){ k = 0;for(i = 0; i < t; i++){ cin>>s;for(j = 0; j < k; j++){ if(s.compare(color[j...