201412-2 Z-shaped scanning Java


Thought to be supplemented

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[][] = new int[n][n];
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for(int i=0;i<2*n-1;i++) {//2n-1条斜线
            int s = i<n?0:(i-n+1);
            int e = i<n?i:(n-1);
            if(i%2 == 0) {//偶数从左下到右上,第一条斜线是0
                for(int j=s;j<=e;j++) {
                    System.out.print(a[i-j][j] + " ");
                }
            }else {
                for(int j=s;j<=e;j++) {
                    System.out.print(a[j][i-j] + " ");
                }
            }
        }
        sc.close();
    }
}

Guess you like

Origin www.cnblogs.com/yu-jiawei/p/12343214.html