C#的谜团解决思路
C#的谜团
程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace 调试
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
int[] maxValIndices;
int maxVal = Maxima(testArray, out maxValIndices);
Console.WriteLine("Maximum{0}", maxVal);
foreach (int index in maxValIndices)
{
Console.WriteLine(index);
}
Console.ReadKey();
}
static int Maxima(int[] integers, out int[] indices)
{
indices = new int[1];
int maxVal = integers[0];
indices[0] = 0;
int count = 1;
for (int i = 1; i < integers.Length; i++)
{
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices = new int[count];
oldIndices.CopyTo(indices, 0);
indices[count - 1] = i;
}
}
}
return maxVal;
}
}
}
我的问题是Main函数里的那个foreach里的maxValIndices的数组长度应该是2啊,怎么运行下来只有一个数字11啊、
------解决思路----------------------
干嘛甩锅给c#,你写的这个Maxima究竟想干嘛才是个谜团
程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace 调试
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
int[] maxValIndices;
int maxVal = Maxima(testArray, out maxValIndices);
Console.WriteLine("Maximum{0}", maxVal);
foreach (int index in maxValIndices)
{
Console.WriteLine(index);
}
Console.ReadKey();
}
static int Maxima(int[] integers, out int[] indices)
{
indices = new int[1];
int maxVal = integers[0];
indices[0] = 0;
int count = 1;
for (int i = 1; i < integers.Length; i++)
{
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices = new int[count];
oldIndices.CopyTo(indices, 0);
indices[count - 1] = i;
}
}
}
return maxVal;
}
}
}
我的问题是Main函数里的那个foreach里的maxValIndices的数组长度应该是2啊,怎么运行下来只有一个数字11啊、
------解决思路----------------------
干嘛甩锅给c#,你写的这个Maxima究竟想干嘛才是个谜团