顺时针打印矩阵
来源:互联网 发布:linux文本替换命令 编辑:程序博客网 时间:2024/05/18 15:24
顺时针打印矩阵各元素
输入:矩阵的行数M、列数N、矩阵各元素的值
输出:有外到内,按顺时针打印矩阵
/* The snake matrix: output a two-dimensional matrix, the matrix elements are output from the outside to the inside * The direction is made: up north down south, left west right east * The direction of each cycle is: 1 to the east 2 to the south 3 to the west 4 to the north * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * rectangle has four coordinates:left,right,up,down*/import java.util.Scanner;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); try{ int M = input.nextInt();//rows int N = input.nextInt();//columns if(M>0 && M<100 && N>0 && N<100){ if(M ==1){ int[] arr = new int [N]; for(int j=0;j<N;j++) arr[j] = input.nextInt(); for(int j=0;j<N;j++) System.out.print(arr[j]+" "); System.out.println(); } else if(N ==1 && M !=1){ int[] arr = new int [M]; for(int i=0;i<M;i++) arr[i] = input.nextInt(); for(int i=0;i<M;i++) System.out.print(arr[i]+" "); System.out.println(); } else{ int[][] array = new int[M][N]; for(int i=0;i<M;i++){ for(int j=0;j<N;j++){ array[i][j] = input.nextInt(); } } int count = 0; int left = 0; int right = N; int up = 0; int down = M; while(count < (M*N)){ for(int j=left;j<right-1 && left<right;j++){ System.out.print(array[up][j]+" "); count++; if(count>=(M*N)) break; } for(int i=up;i<down-1 && up<down;i++){ System.out.print(array[i][right-1]+" "); count++; if(count>=(M*N)) break; } for(int j=right-1;j>left && left<right;j--){ System.out.print(array[down-1][j]+" "); count++; if(count>=(M*N)) break; } for(int i=down-1;i>up && up<down;i--){ System.out.print(array[i][left]+" "); count++; if(count>=(M*N)) break; } left++; right--; up++; down--; } } } else System.out.println("Input error!"); }finally{ input.close(); } }}
阅读全文
0 0
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 矩阵顺时针打印
- 顺时针打印矩阵
- 顺时针打印矩阵[算法]
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- POJ 1385 Lifting the Stone 笔记
- Unity 多人连网(Multiplayer Networking)简单个人总结,玩家独立相机跟踪
- GDB常用命令
- OpenCV3.0+VS2013+Win10环境配置
- SpringBoot 启动解析
- 顺时针打印矩阵
- Matlab 用于求解无约束非线性规划的函数有:fminsearch和fminunc,用法介绍如下。
- hibernate-基础配置-3
- pandas数据分组和聚合操作
- Unity读取windows和Android SDcard文件
- #HDU3530#Subsequence(单调队列)
- Maven的pom.xml介绍(转)
- A Bug's Life POJ
- VIM使用技巧