日期:2014-05-18 浏览次数:20794 次
int c = 所点数字; int x = c % 4; //取所在列 int y = Math.Ceiling(c / 4); //取所在行 int[] r; r[0] = ((y - 2) * 4) + x; //上数 r[1] = ((y - 0) * 4) + x; //下数 r[2] = ((y - 1) * 4) + x - 1; //左数 r[3] = ((y - 1) * 4) + x + 1; //右数 r[4] = ((y - 2) * 4) + x - 1; //上左数 r[5] = ((y - 2) * 4) + x + 1; //上右数 r[6] = ((y - 0) * 4) + x - 1; //下左数 r[7] = ((y - 0) * 4) + x + 1; //下右数 for (int i = 0; i < r.Length; i++) { if (r[i] % 4 != 0 && (r[i] - 1) % 4 != 0 && r[i] > 0 && r[i] < 16) { // 设置r[i]的颜色 } }
------解决方案--------------------
static void Main(string[] args) { Console.WriteLine(fun(2, 3)); } static string fun(int x, int y) { string s = null; int[,] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; for(int i = x - 1;i < x + 2;i++) for (int j = y - 1; j < y + 2; j++) { if (i < 0 || j < 0 || i > 3 || j > 3) continue; if (i == x && j == y) s += a[i, j] + " = red\n"; else s += a[i, j] + " = green\n"; } return s; }