问个问题:整数数组

anycool 2006-09-28 07:50:05
整数数组a[0].....a[n]
y=a[k]+...a[i]
0<=k<=i<=n

求y的最大值


...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
麻文强 2006-09-28
  • 打赏
  • 举报
回复
这应该是求最大连续子序列的问题吧

就是在数组a[n]所有的连续子序列中a[k]...a[i](0<=k<=i<n)哪个子序列的和最大
我有个c程序,你看着改成c#吧

#include <stdio.h>
#include <stdlib.h>

double find_subserial(double x[],int n)
{
double global_max = 0; //save the global max value
double suffix_max = 0; //save the suffix max value
int i;
for(i=0;i<n;i++)
{
if( suffix_max + x[i] > global_max)
{
global_max = suffix_max = suffix_max + x[i];
}
else if( suffix_max + x[i] > 0 )
{
suffix_max = suffix_max+x[i];
}
else
{
suffix_max = 0;
}
}
return global_max;
}

int main(int argc,char * argv[])
{
double * temp;
int i;
temp = (double *)malloc( (argc-1) * sizeof(double) );
for(i=1;i<argc;i++)
{
temp[i-1] = atof(argv[i]);
printf("%g ",temp[i-1]);
}
printf("\n");
printf("The max is %g \n",find_subserial(temp,argc-1));
return 0;
}
ralpha08 2006-09-28
  • 打赏
  • 举报
回复
明白了
int sum=0;
foreach(int i in a)
{
sum+=i;
}
MessageBox.Show(sum.ToString());


sum就是你要求的和
anycool 2006-09-28
  • 打赏
  • 举报
回复
我自己都没看明白,这是一个朋友让我帮忙的。

连他自己都说不清楚
ralpha08 2006-09-28
  • 打赏
  • 举报
回复
看不懂

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧