[BOJ] 6064 카잉달력.java

전영서·2021년 8월 29일
0

Algorithm

목록 보기
16/89

1. 문제

2. 코드

Skip to content
Search or jump toPull requests
Issues
Marketplace
Explore
 
@Mint-Candy95 
Mint-Candy95
/
Algorithm
1
00
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Settings
Algorithm/Baekjoon/Silver/6064_카잉 달력.java /
@Mint-Candy95
Mint-Candy95 Create 6064_카잉 달력.java
Latest commit 7ae23b9 1 minute ago
 History
 1 contributor
60 lines (56 sloc)  1.44 KB
  
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

/**
 * Author : YoungSeo Jeon
 * Date : 2021-08-29
 * Description : 백준 6064
 */

public class Main{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		//테스트 케이스
		int t = Integer.parseInt(br.readLine());
		
	out : for(int tc = 0; tc<t; tc++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			// 입력값 입력
			int M = Integer.parseInt(st.nextToken());
			int N = Integer.parseInt(st.nextToken());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			
			int rx=x;
			int ry=x;
			while(rx>M)
				rx-=M;
			while(ry>N)
				ry-=N;
			int count = x;
			while(true) {
        //정답 조건
				if(rx==x && ry==y) break;
        //우선 하나의 조건이라도 만족해야 하기에 rx에 기준을 맞춰서 x가 되게 횟수를 늘려주었다.
        count += M;
				rx+=M;
				ry+=M;
				while(rx>M)
					rx-=M;
				while(ry>N)
					ry-=N;
				//세상이 끝나는 해
				if(count>M*N) {
					System.out.println(-1);
					continue out;
				}
			}
			System.out.println(count);
		}
		bw.flush();
		bw.close();
		br.close();
	}
}
© 2021 GitHub, Inc.
Terms
Privacy
Security
Status
Docs
Contact GitHub
Pricing
API
Training
Blog
About
Loading complete

3.Review

처음엔 전부 탐색했다가 시간초과가 나와서 해당 방법으로 풀이하였다.

우선 rx는 만족해야 하기에 rx를 기준으로 count를 늘려주었다.

탐색해야하는수가 엄청 줄어들었다.

profile
꾸준히 성실하게

0개의 댓글