分治法求最大元最小元问题~~

guominnj 2010-03-22 07:46:13
using System;
using System.Collections.Generic;
using System.Text;

namespace MaxAndMin
{
class Program
{
int Max1, Min1, max, min;
public void MaxMin(int[] l, int i, int j, ref int Max, ref int Min)
{

if (i == j)
{
Max = Min = l[i];
}
else if (i == j - 1)
{
if (l[i] < l[j])
{
Max = l[j]; Min = l[i];
}
else
{
Max = l[i]; Min = l[j];
}
}
else
{
int m = (i + j) / 2;
MaxMin(l, i, m, ref max, ref min);
MaxMin(l, m + 1, j, ref Max1, ref Min1);
if (max < Max1) max = Max1;
if (min > Min1) min = Min1;
}

}
static void Main(string[] args)
{
int max1 = 0, min1 = 0;
Program p = new Program();
int[] array = new int[5] { 10, 43, 26, 64, 37 };
p.MaxMin(array, 0, array.Length - 1, ref max1, ref min1);
Console.WriteLine("The max is :{0}", max1);
Console.WriteLine("The min is :{0}", min1);
Console.ReadLine();
}
}
}


此程序得到结果是0,0~~但是又不知道是不是c#里的ref用法有错,请高手帮忙
...全文
241 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,499

社区成员

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

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

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