AcWing 3208. Z字形扫描-Java解法
原题链接
简单
作者:
zlnnjit
,
2021-02-16 12:11:52
,
所有人可见
,
阅读 399
import java.util.Scanner;
import java.util.Stack;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] nums = new int[n][n];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) nums[i][j] = sc.nextInt();
Stack<Integer> stack = new Stack<>();
//打印前半部分
int x = -1, y = 0;
boolean up = false;
while (x <= n - 1 && y < n - 1) {
if (x < n - 1) ++x;
else ++y;
if (up) {
//斜向上
int a = y, b = x;
while (b >= 0 && a < n) System.out.print(nums[a++][b--] + " ");
up = false;
}else {
int a = x, b = y;
while (a >= 0 && b < n) System.out.print(nums[a--][b++] + " ");
up = true;
}
}
}
}