백준

백준 6064 카잉 달력(Java)

Park DJ 2023. 2. 18. 19:02

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

 

6064번: 카잉 달력

입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 한 줄로 구성된다.

www.acmicpc.net


 

 

 

Java 코드

 


import java.util.Scanner;

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

    for(int i = 0; i < t; i++) {
      int m = sc.nextInt();
      int n = sc.nextInt();
      int x = sc.nextInt();
      int y = sc.nextInt();
      int num = 0;
      int max = m * n;
      
      if(y == n) y = 0;

      while(true) {
        if(m * num + x > max) {
          sb.append(-1+"\n");
          break;
        }
        if((m * num + x) % n == y) {
          sb.append(m * num + x).append("\n");
          break;
        }
        num++;
      }
    }
    
    System.out.println(sb);
  }
}