本觉得没必要写了,看到网上好像有些java的代码超时了
蓝桥杯基础练习:回形取数
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String args[]){ 5 Scanner sc = new Scanner(System.in); 6 int m = sc.nextInt(); 7 int n = sc.nextInt(); 8 int[][] array = new int[m][n]; 9 for(int i=0;i<m;i++){ 10 for(int j=0;j<n;j++){ 11 array[i][j] = sc.nextInt(); 12 } 13 } 14 15 int tot = 0,x=-1,y=0; 16 while(tot<m*n){ 17 while(x+1 < m && array[x+1][y]!=-1){ 18 System.out.print(array[++x][y]+" "); 19 array[x][y] = -1; 20 ++tot; 21 } 22 while(y+1 < n && array[x][y+1] != -1){ 23 System.out.print(array[x][++y]+" "); 24 array[x][y] = -1; 25 ++tot; 26 } 27 while(x-1>=0 && array[x-1][y]!=-1){ 28 System.out.print(array[--x][y]+" "); 29 array[x][y] = -1; 30 ++tot; 31 } 32 while(y-1>=0 && array[x][y-1]!=-1){ 33 System.out.print(array[x][--y]+" "); 34 array[x][y] = -1; 35 ++tot; 36 } 37 } 38 } 39 40 }
原文:https://www.cnblogs.com/AIchangetheworld/p/12513885.html