Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么总是runtime error!

Posted by Just_do_it at 2008-05-22 09:21:24 on Problem 2479
刚刚用最大子段和写了个求解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",&times);
	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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator