코딩테스트/자바 문제풀이

[CLASS 3: 브루트포스] 백준 6064 카잉 달력

승요나라 2024. 11. 18. 23:38

6064번: 카잉 달력

https://www.acmicpc.net/problem/6064

 

# 코드

import java.util.Scanner;

public class Main {

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

		for (int s = 0; s < T; s++) {

			boolean check = false;
			int m = sc.nextInt();
			int n = sc.nextInt();
			int x = sc.nextInt() - 1;
			int y = sc.nextInt() - 1;

			for (int i = x; i < (n * m); i += m) {
				if (i % n == y) {
					System.out.println(i + 1);
					check = true;
					break;
				}
			}

			if (!check) {
				System.out.println(-1);

			}
		}
	}
}