티스토리 뷰
코드
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 16 17 18
24 23 22 21 20 19
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
boolean flag = true;
int[] arr = new int[k + 1];
for (int i = 1; i <= n * k; i += k) {
if (flag) {
insertForward(arr, k, i);
flag = false;
} else {
insertReverse(arr, k, i + k - 1);
flag = true;
}
}
System.out.print("#" + t + " ");
for (int i = 1; i <= k; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
static void insertForward(int[] arr, int k, int num) {
for (int i = 1; i <= k; i++) {
arr[i] += num++;
}
}
static void insertReverse(int[] arr, int k, int num) {
for (int i = 1; i <= k; i++) {
arr[i] += num--;
}
}
}