矩阵的是比较常用的,尤其是matlab中。数据结构中也介绍过几种特殊的矩阵,但是怎么去实现它呢?看上去很简单,但是真正去实现还是有点困难的,这类题目也可能是很难得,必须认识到规律才好做出来。
下面介绍几种我见过的比较常见的矩阵,以5阶为例,后面会有扩展练习之类的,附上主要代码,仅供参考。
1.一般矩阵
2.S形矩阵
3.Z形矩阵
4.回形矩阵
一般的Z形和回形矩阵被称作蛇形矩阵。
上面四种的常见代码如下:
import java.util.Scanner;/** * @author WANGXIN by 2014-4-17下午4:46:51 * @version v1.0 TODO */public class TestMatrix { static int[][] a; static int t = 1; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入一个数(代表矩阵的规模):"); int n = Integer.valueOf(input.nextLine()); matrix1(n); matrix2(n); matrix3(n); matrix4(n); } // 正常矩阵 public static void matrix1(int n) { a = new int[n][n]; t = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = t++; } } System.out.println("\n\n一般矩阵"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } // S形矩阵 public static void matrix2(int n) { a = new int[n][n]; t = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i % 2 == 0) { a[i][j] = t++; } else { a[i][n - j - 1] = t++; } } } System.out.println("\n\nS形矩阵"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } // 回形矩阵 public static void matrix3(int n) { System.out.println("\n\n回形矩阵:"); t = 1; a = new int[n][n]; matrix3_2(1, 0, 0, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } public static void matrix3_2(int tag, int x, int y, int num) { if (t > a.length * a.length) { return; } if (tag == 1) { for (int i = y; i < y + num; i++) { a[x][i] = t++; } matrix3_2(tag + 1, x + 1, y + num - 1, num - 1); } else if (tag == 2) { for (int i = x; i < x + num; i++) { a[i][y] = t++; } matrix3_2(tag + 1, x + num - 1, y - 1, num); } else if (tag == 3) { for (int i = y; i > y - num; i--) { a[x][i] = t++; } matrix3_2(tag + 1, x - 1, y - num + 1, num - 1); } else if (tag == 4) { for (int i = x; i > x - num; i--) { a[i][y] = t++; } matrix3_2(tag - 3, x - num + 1, y + 1, num); } } // Z形矩阵 public static void matrix4(int n) { a = new int[n][n]; t = 1; int i = 0; int j = 0; a[0][0] = t++; for (; t <= n * n;) { if (i < n && i - 1 >= 0 && j + 1 < n && a[i - 1][j + 1] == 0) { a[i - 1][j + 1] = t++; i -= 1; j += 1; } else if (i + 1 < n && j < n && j - 1 >= 0 && a[i + 1][j - 1] == 0) { a[i + 1][j - 1] = t++; i += 1; j -= 1; } else if (j == 0 && i + 1 < n) { a[i + 1][j] = t++; i += 1; } else if (i == 0 && j + 1 < n) { a[i][j + 1] = t++; j += 1; } else if (i == n - 1 && j + 1 < n) { a[i][j + 1] = t++; j += 1; } else if (j == n - 1 && i + 1 < n) { a[i + 1][j] = t++; i += 1; } } System.out.println("\n\n\nZ形矩阵:"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } }}
5.变形
6.扩展
5,6需要思考如何实现,其中6比较难,需要转化为相应的矩阵,且必须能看出规律。
其实6的代码在算法,蛇形矩阵中已经给出过。
import java.util.Scanner;/** * @author WANGXIN by 2014-3-29下午1:21:48 * @version v1.0 */public class SnakeMatrix { public static void main(String[] args) { int M; int N; System.out .println("Please input the Snake-Matrix`s rows and columns: "); System.out.print("rows = "); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); M = scanner.nextInt(); System.out.print("columns = "); N = scanner.nextInt(); System.out.println("Snake-Matrix(" + M + " , " + N + ")"); printSnake(M, N); } /** * display the Snake-Matrix * * @param M * the row of the Matrix * @param N * the columns of the Matrix */ public static void printSnake(int M, int N) { int num = 0; int i = 0; int j = 0; int x = 0; int y = 0; int[][] arr = new int[M][N]; char[][] str = new char[M][N]; int min = M > N ? N : M; min = (int)Math.round(min / 2.0 + 0.5); while (true) { for (i = x; i <= M - 1 - x; i++) { if (num == M * N) break ; arr[i][N - 1 - y] = ++num; str[i][N - 1 - y] = '↓'; } y++;// decrease y line(s) for (j = N - 1 - y; j >= y - 1; j--) { if (num == M * N) break ; arr[M - y][j] = ++num; str[M - y][j] = '←'; } x++;// decrease x line(s) for (i = M - 1 - x; i >= x - 1; i--) { if (num == M * N) break ; arr[i][y - 1] = ++num; str[i][y - 1] = '↑'; } for (j = y; j < N - y; j++) { if (num == M * N) break ; arr[y - 1][j] = ++num; str[y - 1][j] = '→'; } if (y == min || x == min) {// System.out.println("x = " + x + " y = "+ y);//label is not the most efficient break; } } System.out.println(); for (int k = 0; k < M; k++) { for (int k2 = 0; k2 < N; k2++) { System.out.print(arr[k][k2] + "\t"); } System.out.println(); } System.out.println(); System.out.println("-----------direction show as follow------------"); for (int k = 0; k < M; k++) { for (int k2 = 0; k2 < N; k2++) { System.out.print(str[k][k2] + "\t"); } System.out.println(); } }}