原题链接
In Omkar’s last class of math, he learned about the least common multiple, or LCM. LCM(a,b) is the smallest positive integer x which is divisible by both a and b.
Omkar, having a laudably curious mind, immediately thought of a problem involving the LCM operation: given an integer n, find positive integers a and b such that a+b=n and LCM(a,b) is the minimum value possible.
Can you help Omkar solve his ludicrously challenging math problem?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10). Description of the test cases follows.
Each test case consists of a single integer n (2≤n≤109).
Output
For each test case, output two positive integers a and b, such that a+b=n and LCM(a,b) is the minimum possible.
Example
inputCopy
3
4
6
9
outputCopy
2 2
3 3
3 6
Note
For the first test case, the numbers we can choose are 1,3 or 2,2. LCM(1,3)=3 and LCM(2,2)=2, so we output 2 2.
For the second test case, the numbers we can choose are 1,5, 2,4, or 3,3. LCM(1,5)=5, LCM(2,4)=4, and LCM(3,3)=3, so we output 3 3.
For the third test case, LCM(3,6)=6. It can be shown that there are no other pairs of numbers which sum to 9 that have a lower LCM.
题意
给定t组样例,每组样例给你你个数n,然后输出两个数a,b
a,b满足的条件:a+b=n,而且使LCM(a,b)最小,之后使得Max(a,b)最小
思路
对于偶数来说,直接n/2肯定是最优的结果
对于奇数来说,如果n是素数,就直接输出1,n-1
如果不是,就找到一个最小的质因子
AC代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println(n / 2 + " " + n / 2);
} else {
int ans = 0;
for (int i = 3; i * i <= n; i++) {
if (n % i == 0) {
ans = i;
break;
}
}
if (ans == 0) {
System.out.println(1 + " " + (n - 1));
} else {
System.out.println(n / ans + " " + (n - n / ans));
}
}
}
}
}