对于只会百度的人也要尤其注意题目,面试题要求采用 public string Get(int m, int n) 的函数形式,而不要抄个什么"打印"出来。而且题目要求“显示页面执行时间”,这也是考你是否懂得什么叫“页面”。如果你弄个字符串中用 \r\n 换行,就非常外行了。打印25个数字很简单,更重要地是你理解对题目。不要幼稚地只看到“螺旋矩阵”这个词儿,而忘记了人家考你什么理解能力。
写一个“问题1”的解答好了public static string Get(int m, int n) { var array = new int[n, n]; var c = n / 2; int x = c, y = c; array[x, y] = m++; x++; //右移一位 for (var level = 1; level <= n / 2; level++) { for (; y <= c + level; y++) //从起点开始向下移动 array[x, y] = m++; for (x--, y--; x >= c - level; x--) //先向/左上移一位,然后向左移动 array[x, y] = m++; for (x++, y--; y >= c - level; y--) //先向右上移一位,然后向上移动 array[x, y] = m++; for (x++, y++; x <= c + level; x++) //先向右下移一位,然后向右移动 array[x, y] = m++; }; var sb = new StringBuilder("<html><body>"); for (var j = 0; j <= array.GetUpperBound(1); j++) { for (var i = 0; i <= array.GetUpperBound(0); i++) sb.Append(string.Format("{0:00} ", array[i, j])); sb.Append("<br />"); } return sb.ToString(); }
嗯,输出的不是正确地html,改一下 var sb = new StringBuilder();这样才可以放到aspx中。
using System; using System.Collections.Generic; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int in_1 = Convert.ToInt32(Console.ReadLine()); int in_2 = Convert.ToInt32(Console.ReadLine()); int[] r_1 = new int[] { 1, in_2-1 }; int[] r_2 = new int[] { 0, 0 }; int[] r_3 = new int[] { in_2-1, 0 }; int[] r_4 = new int[] { in_2-1, in_2-1 }; int nowfx = 0;//0左1下2右3上 int maxnum = in_2 * in_2 + in_1-1; int[] stp = new int[] { 0, in_2-1 }; int[,] resnums = new int[in_2, in_2]; for (int i = maxnum; i >= in_1; i--) { if (nowfx == 0)//左 { resnums[stp[0], stp[1]] = i; if (i == in_1) break; if (stp[0] == r_2[0] && stp[1] == r_2[1]) { r_2[0] = r_2[0] + 1; r_2[1] = r_2[1] + 1; nowfx++; c_down(ref stp); continue; } else { c_left(ref stp);//更新坐标 continue; } } if (nowfx == 1) { resnums[stp[0], stp[1]] = i; if (i == in_1) break; if (stp[0] == r_3[0] && stp[1] == r_3[1]) { r_3[0] = r_3[0]-1; r_3[1] = r_3[1]+1; nowfx++; c_right(ref stp); continue; } else { c_down(ref stp);//更新坐标 continue; } } if (nowfx == 2) { resnums[stp[0], stp[1]] = i; if (i == in_1) break; if (stp[0] == r_4[0] && stp[1] == r_4[1]) { r_4[0] = r_4[0]-1; r_4[1] = r_4[1]-1; nowfx++; c_up(ref stp); continue; } else { c_right(ref stp);//更新坐标 continue; } } if (nowfx == 3) { resnums[stp[0], stp[1]] = i; if (i == in_1) break; if (stp[0] == r_1[0] && stp[1] == r_1[1]) { r_1[0] = r_1[0]+1; r_1[1] = r_1[1]-1; nowfx = 0; c_left(ref stp); continue; } else { c_up(ref stp);//更新坐标 continue; } } } for (int i = 0; i < in_2; i++) { for (int x = 0; x < in_2; x++) { if (resnums[i, x] > 9) Console.Write(" " + resnums[i, x]); else Console.Write(" " + resnums[i, x]); } Console.Write("\r\n"); } Console.ReadLine(); } private static void c_left(ref int[] x) { x[1] = x[1]-1; return; } private static void c_down(ref int[] x) { x[0] = x[0]+1; return; } private static void c_right(ref int[] x) { x[1] = x[1]+1; return; } private static void c_up(ref int[] x) { x[0] = x[0]-1; return; } } } 直接反向思维 从49开始计算往左碰到0,0 就往下更新坐标 1,1(内框会用到) 碰到6,0 往右以此类推到指定数字结束 输入第一个数字为开始数字第二个数字为维数
我从来没有扯淡过给别人要求“写出性能测试代码”这种要求。当然别人给我说过很多,我都是一笑了之。后面的解释就非常直接了:显示页面执行时间。这个可以做到,而且也不用写任何代码,直接将aspx页面顶部定义Trace属性就行了。
本帖最后由 rucgis 于 2013-01-12 15:35:32 编辑
这个是他修改以后我看见的,所以我们看见的不一样。
找出下图的规律,用C# 实现 public string Get(int m, int n) 其中m为中心值,n为层数,例如输入(1,5) 输出以下表格:
1)输入m,n,输出结果
2)写出性能测试代码,显示页面执行时间,当n数字很大时,尽可能对算法进行性能优化21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13求高手指点,改怎么写呢?
{
var array = new int[n, n];
var c = n / 2;
int x = c, y = c;
array[x, y] = m++;
x++; //右移一位
for (var level = 1; level <= n / 2; level++)
{
for (; y <= c + level; y++) //从起点开始向下移动
array[x, y] = m++;
for (x--, y--; x >= c - level; x--) //先向/左上移一位,然后向左移动
array[x, y] = m++;
for (x++, y--; y >= c - level; y--) //先向右上移一位,然后向上移动
array[x, y] = m++;
for (x++, y++; x <= c + level; x++) //先向右下移一位,然后向右移动
array[x, y] = m++;
};
var sb = new StringBuilder("<html><body>");
for (var j = 0; j <= array.GetUpperBound(1); j++)
{
for (var i = 0; i <= array.GetUpperBound(0); i++)
sb.Append(string.Format("{0:00} ", array[i, j]));
sb.Append("<br />");
}
return sb.ToString();
}
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int in_1 = Convert.ToInt32(Console.ReadLine());
int in_2 = Convert.ToInt32(Console.ReadLine()); int[] r_1 = new int[] { 1, in_2-1 };
int[] r_2 = new int[] { 0, 0 };
int[] r_3 = new int[] { in_2-1, 0 };
int[] r_4 = new int[] { in_2-1, in_2-1 }; int nowfx = 0;//0左1下2右3上 int maxnum = in_2 * in_2 + in_1-1;
int[] stp = new int[] { 0, in_2-1 }; int[,] resnums = new int[in_2, in_2]; for (int i = maxnum; i >= in_1; i--)
{
if (nowfx == 0)//左
{
resnums[stp[0], stp[1]] = i;
if (i == in_1)
break;
if (stp[0] == r_2[0] && stp[1] == r_2[1])
{
r_2[0] = r_2[0] + 1;
r_2[1] = r_2[1] + 1;
nowfx++;
c_down(ref stp);
continue;
}
else
{
c_left(ref stp);//更新坐标
continue;
}
}
if (nowfx == 1)
{
resnums[stp[0], stp[1]] = i;
if (i == in_1)
break;
if (stp[0] == r_3[0] && stp[1] == r_3[1])
{
r_3[0] = r_3[0]-1;
r_3[1] = r_3[1]+1;
nowfx++;
c_right(ref stp);
continue;
}
else
{
c_down(ref stp);//更新坐标
continue;
}
}
if (nowfx == 2)
{
resnums[stp[0], stp[1]] = i;
if (i == in_1)
break;
if (stp[0] == r_4[0] && stp[1] == r_4[1])
{
r_4[0] = r_4[0]-1;
r_4[1] = r_4[1]-1;
nowfx++;
c_up(ref stp);
continue;
}
else
{
c_right(ref stp);//更新坐标
continue;
}
}
if (nowfx == 3)
{
resnums[stp[0], stp[1]] = i;
if (i == in_1)
break;
if (stp[0] == r_1[0] && stp[1] == r_1[1])
{
r_1[0] = r_1[0]+1;
r_1[1] = r_1[1]-1;
nowfx = 0;
c_left(ref stp);
continue;
}
else
{
c_up(ref stp);//更新坐标
continue;
}
}
}
for (int i = 0; i < in_2; i++)
{
for (int x = 0; x < in_2; x++)
{
if (resnums[i, x] > 9)
Console.Write(" " + resnums[i, x]);
else
Console.Write(" " + resnums[i, x]);
}
Console.Write("\r\n");
}
Console.ReadLine();
} private static void c_left(ref int[] x)
{
x[1] = x[1]-1;
return;
}
private static void c_down(ref int[] x)
{
x[0] = x[0]+1;
return;
}
private static void c_right(ref int[] x)
{
x[1] = x[1]+1;
return;
}
private static void c_up(ref int[] x)
{
x[0] = x[0]-1;
return;
}
}
}
直接反向思维
从49开始计算往左碰到0,0 就往下更新坐标 1,1(内框会用到) 碰到6,0 往右以此类推到指定数字结束
输入第一个数字为开始数字第二个数字为维数