要什么分治法?花里胡哨的,我自己写的代码直接一次循环就够了int zuida=0;//最大的数字int shishi=0;//实时的数字int index1=-1;//存储前索引int index2=...
要什么分治法?花里胡哨的,我自己写的代码直接一次循环就够了
int zuida=0;//最大的数字
int shishi=0;//实时的数字
int index1=-1;//存储前索引
int index2=-1;//存储后索引
int index3=-1;//临时存储的变量
Random num = new Random();//创建骰子器
List list = new List() ;//存储价格波动表
for (int i = 0; i < 15; i++)
{
int res = num.Next(-20, 21);
list.Add(res);
Console.WriteLine(res);
}//随机生成15个价格的波动表并输出
for (int i = 0; i < list.Count; i++)//遍历15次
{
shishi += list[i];
if (shishi<=0)
{
shishi = 0;
index3 = -1;
}
else
{
if (shishi > zuida)
{
zuida = shishi;
if (index3 != -1)
{
index1 = index3;
index2 = i;
}
else
{
index3 = i;
index1 = index3;
}
}
else
{
if (index3 == -1)
{
index3 = i;
}
}
}
}
Console.WriteLine("从第"+index1 + "索引到" + index2+"索引,它的和最大");
Console.ReadKey();
int zuida=0;//最大的数字
int shishi=0;//实时的数字
int index1=-1;//存储前索引
int index2=-1;//存储后索引
int index3=-1;//临时存储的变量
Random num = new Random();//创建骰子器
List list = new List() ;//存储价格波动表
for (int i = 0; i < 15; i++)
{
int res = num.Next(-20, 21);
list.Add(res);
Console.WriteLine(res);
}//随机生成15个价格的波动表并输出
for (int i = 0; i < list.Count; i++)//遍历15次
{
shishi += list[i];
if (shishi<=0)
{
shishi = 0;
index3 = -1;
}
else
{
if (shishi > zuida)
{
zuida = shishi;
if (index3 != -1)
{
index1 = index3;
index2 = i;
}
else
{
index3 = i;
index1 = index3;
}
}
else
{
if (index3 == -1)
{
index3 = i;
}
}
}
}
Console.WriteLine("从第"+index1 + "索引到" + index2+"索引,它的和最大");
Console.ReadKey();