高分求一c#算法,请不吝赐教

来源:百度知道 编辑:UC知道 时间:2024/05/09 20:14:29
请先看图
有一个6*6的方格,依次被标上了1~36的数字
现在想要一个函数
参数是一个包含六个元素的int[]型数组
每个元素的值表示相应的单元格
返回值是一个bool型
判断这六个单元格是否相连
类似图二的情况是相连的
类似图三的情况就是不相连的
我想大家都明白了吧
希望大家用C#来完成
如果代码简介明了,我还会给大家增加分数哦
谢谢大家
ID为 【帮助_li】的那位哥们儿
我用下面代码测试了一下你的Check1方法
结果返回的是false
而实际应该是true
int[] test1 = new int[6] { 12,17,18,22,26,27};
bool result = Check1(test1);
你再瞧瞧?

bool Check(int[] array)
{
int[] tempArray = new int[5];//
int[] disArray = new int[] { 7, 6, 5, 1, -1, -5, -6, -7 };
int temp, index, result, total = tempArray.Length;
Array.Copy(array, 1, tempArray, 0, 5);
for (int i = 1, j = array.Length; i < j; i++)
{
temp = array[i];
foreach (int value in disArray)
{
result = temp - value;
if (result > 0 && result <= 36)
{
index = Array.IndexOf(tempArray, result);
if (index != -1)
{
total--;
tempArray[Array.IndexOf(tempArray,temp)] = 0;
break;
}
}
}
}
return total == 1;
}
//测试
//Check(new int[] { 7,11,14, 21,22,16})==True;
//Check(new int[] { 7,11,14, 21,22,18})==False;
//Check(new int[] { 7,13,14, 22,23,18})==False;
//Check(new int[] { 7,13,14, 21,22,29})==True;

//------以上代码作废

//begin 新代码
//2009-02-13 11:50

bool Check