Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 그래프 자바
- 자바
- 카카오
- 구현
- 스택
- dfs
- java leetcode
- 인텔리제이 에러
- 백준 18222
- 자바 리트코드
- 리트코드 자바
- 프로그래머스 java
- leetcode
- 코딩테스트
- DP
- Java
- 리트코드
- 프로그래머스
- daily challenge
- 리트코드 1557
- 분할정복
- 파이썬
- java 프로그래머스
- 스프링 에러
- 백준 16935
- BFS
- 자바 5464
- leetcode 1721
- 코테
- 백준
Archives
- Today
- Total
레벨업 일지
[Java] 백준 1074 Z 본문
문제
1074번: Z
한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. N > 1인 경우, 배열을
www.acmicpc.net
풀이
반복문으로 구현하였다.
풀이는 다음과 같다.
- 현재 size를 기준으로 (x,y) 가 몇 사분면에 위치하는지 체크한다.
- 정답 변수에 (사분면 * ( size/2 ) * (size/2) ) 을 누적합한다.
- 더 작은 단위로 쪼개기 위해 현재 (행, 열)에 모듈러 연산을 적용한다.
- 정답 변수를 출력한다.
코드
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solution();
}
public void solution() throws IOException {
FastReader fr = new FastReader();
int N;
int r,c;
N = fr.nextInt();
r = fr.nextInt();
c = fr.nextInt();
int maxSize = Math.max(r,c);
long ans = 0;
int size = 1;
while( size <= maxSize){
size *= 2;
}
int d;
int quadrant;
while(size > 1){
d = size/2;
quadrant = (r / d) * 2 + (c / d);
ans += quadrant * d * d;
r %= d;
c %= d;
size /=2;
}
System.out.println(ans);
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] 백준 18222 투에-모스 문자열 (0) | 2023.07.05 |
---|---|
[Java] 백준 17829 222-풀링 (0) | 2023.07.04 |
[Java] 백준 16935 배열 돌리기 3 (0) | 2023.06.29 |
[Java] 백준 10799 쇠막대기 (0) | 2023.06.29 |
[Java] 백준 5464.주차장 (0) | 2023.06.27 |
Comments