昨天去一家公司笔试,只有一道上机题,憋了我两个多小时才憋出来。题目如下,谁有兴趣可以试试看。
题目:输入一个整数(n)作为参数,打印出n*n循环矩阵
例:输入 4 打印出
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
代码如下,第一次原创,欢迎交流
Code
1
2
3using System;
4using System.Collections.Generic;
5using System.Text;
6namespace ConsoleApplication1
7{
8 class Program
9 {
10 static int iResult = 0;
11 static int[,] matrix = new int[1000, 1000];
12 static int itype = 1;
13 static int icount = 1;
14 static void Main(string[] args)
15 {
16 Console.WriteLine("请输入一个整数");
17 try
18 {
19 int aa = 0;
20 while (aa == 0)
21 {
22 aa = int.Parse(Console.ReadLine().ToString());
23 Print(1, aa, 1);
24 iResult = 0;
25 itype = 1;
26 icount = 1;
27 matrix = new int[1000, 1000];
28 aa = 0;
29 }
30 }
31 catch
32 { }
33 Console.WriteLine("结束");
34 Console.ReadLine();
35 }
36 static public void Print(int type, int iTemp, int count)
37 {
38 itype = type % 4;
39 icount = count;
40 for (int i = 1; i <= 4; i++)
41 {
42 for (int j = 1; j <= iTemp - 1 - 2 * (icount - 1); j++)
43 {
44 switch (itype)
45 {
46 case 1:
47 iResult = iResult + 1;
48 matrix[icount + i - 2, icount + j - 2] = iResult;
49 break;
50 case 2:
51 iResult = iResult + 1;
52 matrix[icount + j - 2, iTemp - icount] = iResult;
53 break;
54 case 3:
55 iResult = iResult + 1;
56 matrix[iTemp - icount, iTemp - j + 1 - icount] = iResult;
57 break;
58 case 4:
59 iResult = iResult + 1;
60 matrix[iTemp - j - icount + 1, icount - 1] = iResult;
61 break;
62 }
63 }
64 itype++;
65 }
66 icount++;
67
68
69 if (icount + 2 > iTemp)
70 {
71 if (iTemp % 2 == 1)
72 {
73 matrix[(iTemp + 1) / 2 - 1, (iTemp + 1) / 2 - 1] = iTemp * iTemp;
74 }
75 for (int i = 0; i < iTemp; i++)
76 {
77 for (int j = 0; j < iTemp; j++)
78 {
79 Console.Write(matrix[i, j]);
80 Console.Write(" ");
81 }
82 Console.WriteLine();
83 }
84 return;
85 }
86 Print(itype, iTemp, icount);
87 }
88
89 }
90}
91
1
2
3using System;
4using System.Collections.Generic;
5using System.Text;
6namespace ConsoleApplication1
7{
8 class Program
9 {
10 static int iResult = 0;
11 static int[,] matrix = new int[1000, 1000];
12 static int itype = 1;
13 static int icount = 1;
14 static void Main(string[] args)
15 {
16 Console.WriteLine("请输入一个整数");
17 try
18 {
19 int aa = 0;
20 while (aa == 0)
21 {
22 aa = int.Parse(Console.ReadLine().ToString());
23 Print(1, aa, 1);
24 iResult = 0;
25 itype = 1;
26 icount = 1;
27 matrix = new int[1000, 1000];
28 aa = 0;
29 }
30 }
31 catch
32 { }
33 Console.WriteLine("结束");
34 Console.ReadLine();
35 }
36 static public void Print(int type, int iTemp, int count)
37 {
38 itype = type % 4;
39 icount = count;
40 for (int i = 1; i <= 4; i++)
41 {
42 for (int j = 1; j <= iTemp - 1 - 2 * (icount - 1); j++)
43 {
44 switch (itype)
45 {
46 case 1:
47 iResult = iResult + 1;
48 matrix[icount + i - 2, icount + j - 2] = iResult;
49 break;
50 case 2:
51 iResult = iResult + 1;
52 matrix[icount + j - 2, iTemp - icount] = iResult;
53 break;
54 case 3:
55 iResult = iResult + 1;
56 matrix[iTemp - icount, iTemp - j + 1 - icount] = iResult;
57 break;
58 case 4:
59 iResult = iResult + 1;
60 matrix[iTemp - j - icount + 1, icount - 1] = iResult;
61 break;
62 }
63 }
64 itype++;
65 }
66 icount++;
67
68
69 if (icount + 2 > iTemp)
70 {
71 if (iTemp % 2 == 1)
72 {
73 matrix[(iTemp + 1) / 2 - 1, (iTemp + 1) / 2 - 1] = iTemp * iTemp;
74 }
75 for (int i = 0; i < iTemp; i++)
76 {
77 for (int j = 0; j < iTemp; j++)
78 {
79 Console.Write(matrix[i, j]);
80 Console.Write(" ");
81 }
82 Console.WriteLine();
83 }
84 return;
85 }
86 Print(itype, iTemp, icount);
87 }
88
89 }
90}
91