Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么总是runtime error!刚刚用最大子段和写了个求解maximum sum的程序,思路如下: 用pos表示的位置作为分水岭,求包括pos在内的前一段的最大子段和,再求pos后面的最大子段和两个相加得到一个备选的maximum sum,从pos在1-count-1范围内的每个取值得到的备选maximum sum中挑选最大的就是最后的解 代码如下:希望牛人帮忙看看为什么总是runtime error!小弟先谢谢了! #include <stdio.h> int number[5001],p1[5001],p2[5001]; int main(int argc, char* argv[]) { int i,j,pos,pre,last,ms,count,times; scanf("%d",×); i = 0; while(i < times) { scanf("%d",&count); for(j = 1;j <= count;j++) scanf("%d",&number[j]); if(count == 2) ms = number[1] + number[2]; else { p1[1] = number[1]; for(j = 2;j <= count - 1;j++) { if(p1[j - 1] > 0) p1[j] = p1[j - 1] + number[j]; else p1[j] = number[j]; } ms = -2000; for(pos = 1;pos <= count - 1;pos++) { pre = p1[1]; for(j = 1;j <= pos;j++) { if(pre < p1[j]) pre = p1[j]; } p2[pos + 1] = number[pos + 1]; last = p2[pos + 1]; for(j = pos + 2;j <= count;j++) { if(p2[j - 1] > 0) p2[j] = p2[j - 1] + number[j]; else p2[j] = number[j]; if(last < p2[j]) last = p2[j]; } if(ms < pre + last) ms = pre + last; } } printf("%d\n",ms); i++; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator