티스토리 뷰
1952번: 달팽이2
M줄 N칸으로 되어 있는 표 위에, 달팽이 모양으로 선을 그리려고 한다. 위의 그림은 M=5, N=3의 예이다. 이제 표의 왼쪽 위 칸(ㅇ)에서 시작하여, 오른쪽으로 선을 그려 나간다. 표의 바깥 또는 이미
www.acmicpc.net
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int [][]arr = new int[m][n];
int max = m * n;
int x = m;
int y = n;
int answer = 0;
int i = 0;
int j = -1;
int count = 1;
while(true) {
for(int t = 0; t < n; t++) {
j += count;
arr[i][j] = max;
max--;
}
m--;
if(m <= 0) break;
answer++;
for(int t = 0; t < m; t++) {
i += count;
arr[i][j] = max;
max--;
}
n--;
if(n <= 0) break;
count *= -1;
answer++;
}
System.out.println(answer);
}
}
'algorithm' 카테고리의 다른 글
[algorithm] 그리디 알고리즘 (0) | 2021.04.25 |
---|---|
[algorithm] 그리디 알고리즘 (0) | 2021.04.17 |
[algorithm] 달팽이 java (0) | 2021.04.13 |
[algorithm] 동적프로그래밍 알고리즘 (0) | 2021.04.13 |
동적프로그래밍 알고리즘 (0) | 2021.04.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- stackframe
- client side rendering
- C
- 세마포어
- Java
- 소프트웨어
- 자료구조
- 클래스
- dfs
- 스텍
- 최단경로
- 교착상태
- 배열
- server side rendering
- 구조체
- react
- BFS
- Stack
- 이진탐색
- 재귀함수
- 운영체제
- 인접리스트
- 알고리즘
- 동적프로그래밍
- 입출력장치
- 병행프로세스
- 퀵정렬
- C++
- 인접행렬
- javascript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함