日期:2014-05-20 浏览次数:21002 次
private void button1_Click(object sender, EventArgs e)
{
#region 初始化数组
int[] array = new int[arrayLength];
Random random = new Random(1000); // 固定随机种子,使大家测试数据一致
for (int i = 0; i < array.Length; i++)
array[i] = random.Next(arrayLength * 10);
#endregion 初始化数组
int A, B, C;
long tickCount = Environment.TickCount;
Search(array, out A, out B, out C);
Console.WriteLine("计算结果:A={0} B={1} C={2},耗时:{3}",
A, B, C, Environment.TickCount - tickCount);
}
public void Search(int[] array, out int A, out int B, out int C)
{
A = -1;
B = -1;
C = -1;
/* TODO : 自由发挥 */
}
public void Search(int[] array, out int A, out int B, out int C) { A = -1; B = -1; C = -1; int[] all = new int[arrayLength * 10]; for (int i = 0; i < array.Length; i++) all[array[i]]++; for (int j = all.Length-1; j > 0; j--) { if (all[j] == 0) continue; int num = j; int halfnum=num/2; if (num % 2 == 0) { if (all[halfnum] >= 2) { A = B = halfnum; C = j; return; } } for (int t = 1; t < halfnum; t++) { if (all[t] > 0 && all[num - t] > 0) { A = t; B = num - t; C = num ; return; } } }
------解决方案--------------------
static public void Search(int[] array, out int A, out int B, out int C) { A = -1; B = -1; C = -1; if( array == null || array.Length < 3) return; // O(n*log n) Array.Sort<int>(array); int maxNumber = array[ array.Length-1 ]; bool hasZero = (array[0] == 0); unsafe { byte* masks = stackalloc byte[maxNumber+2]; //if not Microsoft's CLR, DO initialize the allocated stack! //for (int i = 0; i < maxNumber + 2; i++) masks[i] = 0; for(int i=0; i<array.Length; i++) { masks[ array[i] ]++; } // O(n*n) for(int i=array.Length -1; i>=0; i--) { int sum = array[i]; // special case if( hasZero && masks[sum] > 1 ) { A = 0; B = sum; C = sum; return; } int halfSum = sum / 2; for(int j = i-1; j>0 && array[j] >= halfSum; j--) { if (masks[sum - array[j]] > 0) { A = sum - array[j]; B = array[j]; C = sum; if (A != B || masks[A] > 1) return; else { A = B = C = -1; } } } } } // done }