본문 바로가기
Java/알고리즘

[Java] 백준 6064번 : 카잉 달력

by EricJeong 2019. 8. 21.

 

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();

		for (int i = 0; i < T; i++) {
			int answer = 0;
			int M = in.nextInt();
			int N = in.nextInt();
			int x = in.nextInt();
			int y = in.nextInt();
			
			int max = M*N;
			int index = 0;
			
			if(y == N)
				y = 0;
			
			while(true) {
				if((M*index + x) %  N == y) {
					answer = M*index + x;
					break;
				}
				
				if(M*index + x > max) {
					answer = -1;
					break;
				}
				index++;
			}
			
				
			
			System.out.println(answer);
		}
	}
}

 

 

 

 

 

 

댓글