題目連結
題意:
GCD(G) 表示最大公因數,LCM(L)表示最小公倍數。
每組測資給定 G 和 L,求兩數 a, b
其最大公因數與最小公倍數恰為G與L,且 a 為最小,若不存在則輸出 -1
解法:
因此,題目要求兩數之最大公因數等於前者,最小公倍數等於後者
也就是若後者為前者的因數,就輸出 G 與 L,若否則輸出 -1
程式(Java):
題意:
GCD(G) 表示最大公因數,LCM(L)表示最小公倍數。
每組測資給定 G 和 L,求兩數 a, b
其最大公因數與最小公倍數恰為G與L,且 a 為最小,若不存在則輸出 -1
解法:
因此,題目要求兩數之最大公因數等於前者,最小公倍數等於後者
也就是若後者為前者的因數,就輸出 G 與 L,若否則輸出 -1
程式(Java):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
int T = sc.nextInt(); | |
while (T-- > 0) { | |
int G = sc.nextInt(); | |
int L = sc.nextInt(); | |
if(L % G != 0) | |
System.out.println("-1"); | |
else | |
System.out.println(G + " " + L); | |
} | |
} | |
} |
留言
張貼留言