蓝桥杯BASIC_25(回形取数)

问题描述
  回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转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 testing_03;
import java.util.Scanner;
public class test_07 {  
    static void getNum(int m,int n,int[][] num){
        int length=m*n;
        int newNum[]=new int[length];
        int count=0;int x=-1,y=0;
        while(count<length){
             while(x + 1 < m && num[x + 1][y] != -1)  
                {  
                    System.out.print(num[++x][y]);
                    num[x][y] = -1;  
                    ++count;  
                }  
                while(y + 1 < n && num[x][y + 1] != -1)  
                {  
                    System.out.print(num[x][++y]);  
                    num[x][y] = -1;  
                    ++count;  
                }  
                while(x - 1 >= 0 && num[x - 1][y] != -1)  
                {  
                    System.out.print(num[--x][y]); 
                    num[x][y] = -1;  
                    ++count;  
                }  
                while(y - 1 >= 0 && num[x][y - 1] != -1)  
                {  
                    System.out.print(num[x][--y]);
                    num[x][y] = -1;  
                    ++count;  
                }  
            }        
        }
  public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int m=input.nextInt();
    int n=input.nextInt();
    int num[][]=new int[m][n];
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
              num[i][j]=input.nextInt();
           }
       }
       getNum(m,n,num);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值