分治法求最大元最小元问题~~
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用法有错,请高手帮忙