随笔分类 -  编程题

求连续子数组的最大和
摘要:求子数组的最大和题目描述:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18。代码:int maxsum(int *a,int n){ int tmp=0; int max=0; for(int i=0; i<n; i++) { if(tmp<0) tmp=a[... 阅读全文

posted @ 2012-05-19 14:41 王耀it 阅读(130) 评论(0) 推荐(0)

动态规划习题 poj1050
摘要:原题:To the MaxTime Limit:1000MSMemory Limit:10000KTotal Submissions:31305Accepted:16272DescriptionGiven a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of al 阅读全文

posted @ 2012-05-19 13:41 王耀it 阅读(222) 评论(0) 推荐(0)

动态规划习题 poj1163
摘要:原题:The TriangleTime Limit:1000MSMemory Limit:10000KTotal Submissions:28546Accepted:16809Description73 88 1 02 7 4 44 5 2 6 5(Figure 1)Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends som... 阅读全文

posted @ 2012-05-18 17:35 王耀it 阅读(196) 评论(0) 推荐(0)