using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4,7,4,2,7,3,3,7,8,3,9,1,9};
int[] maxValIndices;
int maxVal = Maxi(testArray, out maxValIndices);
Console.WriteLine("Maximum value {0} found at element indices ",maxVal);
foreach (int index in maxValIndices)
{
Console.WriteLine("{0}",index);
}
Console.ReadKey();
}
static int Maxi(int[] integers, out int[] indices)
{
Debug.WriteLine("Maximum Value search started.");
indices = new int[1];
int maxVal=integers[0];
indices[0] = 0;
int count = 1;
int i;
Debug.WriteLine(string.Format("maximum value initialized to {0},at element index 0.", maxVal));
for (i = 1; i < integers.Length; i++)
{
Debug.WriteLine(string.Format("Noe looking at element at index {0}", i));
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = i;
indices = new int[1];
indices[0] = i;
Debug.WriteLine(string.Format("New maximum found.new value is{0},at element index{1}", maxVal, i));
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices = new int[count];
oldIndices.CopyTo(indices, 0);
indices[count - 1] = i;
Debug.WriteLine(string.Format("Duplicate maximum found at element index {0}", i));
}
}
Trace.WriteLine(string.Format("Maximum Value {0} found,with {1} occurrences.", maxVal, count));
Debug.WriteLine(string.Format("Maximum value search completed."));
return maxVal;
}
}
}
}
错误提示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4,7,4,2,7,3,3,7,8,3,9,1,9};
int[] maxValIndices;
int maxVal = Maxi(testArray, out maxValIndices);
Console.WriteLine("Maximum value {0} found at element indices ",maxVal);
foreach (int index in maxValIndices)
{
Console.WriteLine("{0}",index);
}
Console.ReadKey();
}
static int Maxi(int[] integers, out int[] indices)
{
Debug.WriteLine("Maximum Value search started.");
indices = new int[1];