矩阵顺时针旋转

来源:互联网 发布:php socket了解 编辑:程序博客网 时间:2024/05/18 12:39
public class matrixRotation {    public static void rotation(int[][] n,int [][] m,int i,int j){        int t = j;  // 标记最后一行的位置        if(i>=n.length) return;        for(int k=0;k<n.length;k++){            m[i][k] = n[j--][i];    // 解决一行        }        rotation(n,m,++i,t);    // 递归解决下一行    }    // 输出矩阵    public static void print(int[][] t){        for(int[] x: t){            for(int y:x){                System.out.print(y+"\t");            }            System.out.println();        }    }    public static void main(String[] args){        int[][] n = {                {1 ,2 ,3 ,4 },                {5 ,6 ,7 ,8 },                {9 ,10,11,12},                {13,14,15,16}                   };        print(n);   // 显示原矩阵        int len = n.length;        int[][] m = new int[len][len];  // 目标矩阵        rotation(n,m,0,len-1);  // 矩阵顺时针旋转        System.out.println("顺时针旋转结果:");        print(m);   // 显示目标矩阵    }}
0 0
原创粉丝点击