zoukankan      html  css  js  c++  java
  • java几个经典的算法题目----------二维矩阵算法

    public class testClockwiseOutput { 
    	
    	public static void main(String[] args) {
    		//1、构建矩阵数据
    		int[][] arr = getMatrix();
    		matrixSort(arr, 0, 3);
    	}
    	
    	
    	/**
    	 * 构建矩阵的二维数组
    	 * @return
    	 */
    	public static int[][] getMatrix(){
    		//1、创建一个容量为100的二维数组
    		int[][] arr = new int[100][100];
    		//2、定义矩阵的边数为4
    		int n = 4;
    		//3、加入矩阵的整数
    		int count = 1;
    		
    		//4、添加矩阵数据
    		for(int i=0;i < n;i++){
    			for(int j=0;j < n;j++){
    				System.out.print(count+" ");
    				arr[i][j] = count++;
    			}
    			System.out.println("");
    		}
    		return arr;
    	}
    	
    	
    	/**
    	 * 对矩阵进行排序
    	 */
    	public static void matrixSort(int[][] arr,int start,int end){
    		
    		if(start >= end || end < 0){
    			return ;
    		}
    		
    		for(int i=start;i<=end;i++){
    			System.out.print(arr[start][i]);
    		}
    		
    		for(int i=start+1;i<=end;i++){
    			System.out.print(arr[i][end]);
    		}
    		
    		for(int i = end-1;i >=start;i--){
    			System.out.print(arr[end][i]);
    		}
    		
    		for(int i=end-1;i>start;i--){
    			System.out.print(arr[i][start]);
    		}
    		
    		matrixSort(arr, start+1, end-1);
    	}
    

      

  • 相关阅读:
    6月15日学习日志
    6月14日学习日志
    6月13日学习日志
    6月12日学习日志
    给建民哥的意见
    6月10日学习日志
    6月9日学习日志
    6月8日学习日志
    梦断代码读书笔记3
    第二次冲刺(六)
  • 原文地址:https://www.cnblogs.com/hanxue112253/p/9614529.html
Copyright © 2011-2022 走看看