java实现螺旋矩阵、C++实现螺旋矩阵(顺时针、逆时针)

螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,向左变大,向上变大,如此循环。如下图所示:
                                                 java实现螺旋矩阵、C++实现螺旋矩阵(顺时针、逆时针)_第1张图片

            此类题目经常出现在各大互联网公司的面试笔试这种,今天我用c++实现输入一个整N,就打印出N*N的螺旋矩阵。

解题思路:

       这道题刚开始整的感觉还不太好操作,主要就是找规律,主要思路就是按照由外到内,一层一层进行for循环打印,最外层循环控制有多少层,每层分为(上方、右侧、下方、左侧)四个递增的循环,直到最后一层打印完。如果输入N为奇数,将会有N/2 + 1层,最内层为N平方一个数字;如果N为偶数,将会有N/2层。

Java实现源码:

import java.util.Arrays;
import java.util.Scanner;

public class SpiralMatrix {

    private void printArray(int[][] arr) {
        int length = arr.length - 1;
        if (arr.length != 1) {
            for (int i = 1; i <= length; ++i) {
                for (int n, j = 1; j <= length; ++j) {
                    System.out.print(String.format("% -8d", arr[i][j]));
                }
                System.out.println();
            }
        } else {
            System.out.print("input length error!");
        }
    }

    private int[][] getArray(int n) {
        int[][] arr = new int[n + 1][n + 1];
        for (int[] a : arr) {
            Arrays.fill(a, 0);
        }
        return arr;
    }

    //顺时针螺旋
    private void clockwise(int[][] arr) {
        System.out.println("clockwise print: ");
        int i = 0, j = 0, k = 1, n = arr.length - 1, m = n / 2;
        if (n % 2 != 0) arr[m + 1][m + 1] = n * n;

        for (i = 1; i <= n / 2; ++i) {
            //上方
            for (j = i; j <= n - i; ++j) {
                arr[i][j] = k++;
            }

            //右侧
            for (j = i; j <= n - i; ++j) {
                arr[j][n + 1 - i] = k++;
            }

            //下方
            for (j = n - i + 1; j >= i + 1; j = j - 1) {
                arr[n + 1 - i][j] = k++;
            }

            //左侧
            for (j = n - i + 1; j >= i + 1; j = j - 1) {
                arr[j][i] = k++;
            }
        }

    }

    //逆时针螺旋
    private void anticlockwise(int[][] arr) {
        System.out.println("anticlockwise print: ");
        int i = 1, j = 0, k = 1, n = arr.length - 1, m = n / 2;
        if (n % 2 != 0) arr[m + 1][m + 1] = n * n;
        //一层一层去打印,从外往里打印
        for (i = 1; i <= n / 2; ++i) {
            //上方
            for (j = n - i + 1; j > i; --j) {
                arr[i][j] = k++;
            }

            //右侧
            for (j = i; j <= n - i; ++j) {
                arr[j][i] = k++;
            }

            //下方
            for (j = i; j <= n - i; ++j) {
                arr[n + 1 - i][j] = k++;
            }

            //左侧
            for (j = n - i + 1; j >= i + 1; --j) {
                arr[j][n - i + 1] = k++;
            }
        }
    }

    public static void main(String[] argv) {
        SpiralMatrix spiralMatrix = new SpiralMatrix();
        Scanner scanner = new Scanner(System.in);
        System.out.print("please input a number n: ");
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[][] arr = spiralMatrix.getArray(n);
            //顺时针
            spiralMatrix.clockwise(arr);
            spiralMatrix.printArray(arr);

            System.out.println();

            //逆时针
            spiralMatrix.anticlockwise(arr);
            spiralMatrix.printArray(arr);
            System.out.print("please input a number n: ");
        }
    }
}

运行结果:

java实现螺旋矩阵、C++实现螺旋矩阵(顺时针、逆时针)_第2张图片

 

 

C++实现源码:

#include 
#include
#include
using namespace std;

int input() {
	int n = 0;
	cout << "please input a number n:";
	cin >> n;
	return n;
}

void printArray(int n, vector >& arr) {
	if (n != 1) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				cout << setw(5) << arr[i][j];
			}
			cout << endl;
		}
	}
	else {
		cout << setw(5) << n;
	}
}

vector > getArray(int n) {
	vector > arr(n + 1, vector(n + 1));
	return arr;
}

int** getArray1(int n) {
	int** arr = new int* [n];
	for (int i = 0; i < n + 1; i++) {
		arr[i] = new int[n + 1];
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = i; j <= n; ++j)
			arr[i][j] = 0;
	}
	return arr;
}

void deleteArray1(int n, int** a) {
	for (int i = 0; i < n + 1; i++) {
		delete[]a[i];
	}
	a = NULL;
}

//顺时针螺旋
void clockwise(int n, vector>& arr) {
	cout << "clockwise print: " << endl;
	int i = 0, j = 0, k = 1, m = n / 2;
	if (n % 2 != 0) arr[m + 1][m + 1] = n * n;

	for (i = 1; i <= n / 2; ++i) {
		//上方 
		for (j = i; j <= n - i; ++j) {
			arr[i][j] = k++;
		}

		//右侧
		for (j = i; j <= n - i; ++j) {
			arr[j][n + 1 - i] = k++;
		}

		//下方
		for (j = n - i + 1; j >= i + 1; j = j - 1) {
			arr[n + 1 - i][j] = k++;
		}

		//左侧
		for (j = n - i + 1; j >= i + 1; j = j - 1) {
			arr[j][i] = k++;
		}
	}

}

//逆时针螺旋
void anticlockwise(int n, vector >& arr) {
	cout << "anticlockwise print: " << endl;
	int i = 1, j = 0, k = 1, m = n / 2;
	if (n % 2 != 0) arr[m + 1][m + 1] = n * n;
	//一层一层去打印,从外往里打印
	for (i = 1; i <= n / 2; ++i) {
		//上方
		for (j = n - i + 1; j > i; --j) {
			arr[i][j] = k++;
		}

		//右侧
		for (j = i; j <= n - i; ++j) {
			arr[j][i] = k++;
		}

		//下方
		for (j = i; j <= n - i; ++j) {
			arr[n + 1 - i][j] = k++;
		}

		//左侧
		for (j = n - i + 1; j >= i + 1; --j) {
			arr[j][n - i + 1] = k++;
		}
	}
	
}

int main() {
	int n = input();
	vector> arr = getArray(n);
	clockwise(n, arr);
	printArray(n, arr);
	anticlockwise(n, arr);
	printArray(n, arr);
	return 0;
}

运行结果   :
                                                  

java实现螺旋矩阵、C++实现螺旋矩阵(顺时针、逆时针)_第3张图片


 

你可能感兴趣的:(算法,笔试练习题,C++)