1. 문제풀이
- 처음에 2차원 배열로 풀려고 했으나, 1...6 까지 간 후 거꾸로 가는과정에서 어려움이 있었음
- 1차원 배열을 생성 한 후 N번 반복하여 계산하고, flag 를 통해 순서를 바꾸는 방법을 알게 됨
import java.util.Scanner;
public class 조만들기 {
static int[] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt();
int K = sc.nextInt();
int count = 1;
boolean flag = true;
arr = new int[K];
for (int i = 0; i < N; i++) {
if (flag) {
for (int j = 0; j < K; j++) {
arr[j] += count;
count++;
}
flag = false;
} else {
for (int j = K - 1; j >= 0; j--) {
arr[j] += count;
count++;
}
flag = true;
}
}
System.out.print("#" + test_case + " ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
}
'JAVA 알고리즘 ' 카테고리의 다른 글
BOJ 7576번 - 토마토 (1) | 2020.07.08 |
---|---|
SWEA 2805번 - 농작물 수확하기 (1) | 2020.06.27 |
백준 2667번 - 단지번호붙이기 (0) | 2020.06.25 |
백준 1260번 - DFS와 BFS (2) | 2020.06.25 |
백준2573번 - 빙산 (1) | 2020.06.24 |