@java 蓝 桥 杯 B group exercises basic exercises (30) BASIC-025 get back shape number JAVA two-dimensional array get back shape number (counterclockwise)

Problem Description:

The fetching of the round shape is the fetching along the edge of the matrix. If there are countless fetches or the fetches in the current direction, turn left 90 degrees. It is initially located in the upper left corner of the matrix, with the direction down. Input format The first input row is two positive integers m, n not exceeding 200, which represent the rows and columns of the matrix.

Code

/**
 *   Aurora   2020年4月15日
 问题描述
  回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
输入格式
  输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
输出格式
  输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
样例输入
3 3
1 2 3
4 5 6
7 8 9
样例输出
1 4 7 8 9 6 3 2 5
样例输入
3 2
1 2
3 4
5 6
样例输出
1 3 5 6 4 2
 */
package 基础BASIC;
import java.util.ArrayList;
import java.util.Scanner;
public class 回形取数¥二维数组_循环 {
	public static void main(String[] args) {
		ArrayList<Integer> list=new ArrayList<Integer>();
		Scanner s=new Scanner(System.in);
		int m=s.nextInt();//m行
		int n=s.nextInt();//n列
		if(m>0&&m<=200&&n>0&&n<=200){
			int[][] a=new int[m][n];//n列m行的矩阵,表示这个矩阵
			for(int i=0;i<m;i++) {//n列m行的矩阵
				for(int j=0;j<n;j++) {
					a[i][j]=s.nextInt();
				}
			}
			int leftUpRow=0;//定义起点第一行下标(左上角)
			int leftUpCol=0;//定义起点第一列下标(左上角)
			int rightDownRow=m-1;//终点行坐标
			int rightDownCol=n-1;//终点列坐标
			while((leftUpRow<=rightDownRow)&&(leftUpCol<=rightDownCol)) {
				int r=leftUpRow,c=leftUpCol;
				while(r<=rightDownRow) {//打印第一列:
					list.add(a[r][c]);
					r++;
				}
				r--;//防止下标越界
				c++;//防止重复打印
				while(c<=rightDownCol){//打印最后一行
					list.add(a[r][c]);
					c++;
				}
				c--;//防止下标越界
				r--;//防止重复打印
				while(r>=leftUpRow) {//打印最后一列
					list.add(a[r][c]);
					r--;
				}
				r++;//防止下标越界
				c--;//防止重复打印
				while(c>leftUpCol) {//打印第一行
					list.add(a[r][c]);
					c--;
				}
				leftUpCol++;
				leftUpRow++;
				rightDownRow--;
				rightDownCol--;
			}
			for(int i=0;i<list.size();i++) {
				if(i==list.size()-1) {
					System.out.print(list.get(i));
				}else {
					System.out.print(list.get(i)+" ");
				}
				
			}
	}
}}

When I entered the code, I didn't know where there was a problem, and I couldn't get full marks. The results seemed to meet the conditions ~? 0.0 ... I failed to find out, so I have the following code:


import java.util.Scanner;
public class 回形取数$二维数组_循环 {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int m=s.nextInt();//m行
		int n=s.nextInt();//n列
		if(m>0&&m<=200&&n>0&&n<=200){
			int[][] a=new int[m][n];//n列m行的矩阵,表示这个矩阵
			for(int i=0;i<m;i++) {//n列m行的矩阵
				for(int j=0;j<n;j++) {
					a[i][j]=s.nextInt();
				}
			}
			int leftUpRow=0;//定义起点第一行下标(左上角)
			int leftUpCol=0;//定义起点第一列下标(左上角)
			int rightDownRow=m-1;//终点行坐标
			int rightDownCol=n-1;//终点列坐标
			while((leftUpRow<=rightDownRow)&&(leftUpCol<=rightDownCol)) {
				int r=leftUpRow,c=leftUpCol;
				while(r<=rightDownRow) {//打印第一列:
					if((r==rightDownRow&&leftUpRow==rightDownRow)|(r==rightDownRow&&leftUpCol==rightDownCol)) {//如果本次输出为最后一个数
					System.out.print(a[r][c]);
					}else {
					System.out.print(a[r][c]+" ");
					}
					r++;
				}
				r--;//防止下标越界
				c++;//防止重复打印
				while(c<=rightDownCol){//打印最后一行
					if((c==rightDownCol&&leftUpRow==rightDownRow)|(c==rightDownCol&&leftUpCol==rightDownCol)) {//如果本次输出为最后一个数
					System.out.print(a[r][c]);
					}else {
					System.out.print(a[r][c]+" ");
					}
					c++;
				}
				c--;//防止下标越界
				r--;//防止重复打印
				while(r>=leftUpRow) {//打印最后一列
					if((r==leftUpRow&&leftUpRow==rightDownRow)|(r==leftUpRow&&leftUpCol==rightDownCol)) {//如果本次输出为最后一个数
					System.out.print(a[r][c]);
					}else {
					System.out.print(a[r][c]+" ");
					}
					r--;
				}
				r++;//防止下标越界
				c--;//防止重复打印
				while(c>leftUpCol) {//打印第一行
					if((c==leftUpCol&&leftUpRow==rightDownRow)|(c==leftUpCol&&leftUpCol==rightDownCol)) {//如果本次输出为最后一个数
					System.out.print(a[r][c]);
					}else {
					System.out.print(a[r][c]+" ");
					}
					c--;
				}
				leftUpCol++;
				leftUpRow++;
				rightDownRow--;
				rightDownCol--;
			}
	}
}}

The test results can not get full marks ... please ask the gods to give instructions ...

Now
Published 41 original articles · Like1 · Visits 1290

Guess you like

Origin blog.csdn.net/DAurora/article/details/105546992