一、Problem
我們所熟知的、以及未知的世界,在破碎的夢想面前不堪一擊。所有的一切,都面目全非……
在這個世界中,有我們熟知的質數,但對於質數,我們還有很多很多未知……
所以,wlxsq不打算研究質數,他打算研究合數。
他想知道,是否存在兩個數 a 和 b,滿足 a 和 b 的差爲 n,並且 a 和 b 均爲合數,你能幫幫他嗎?
輸入
輸入一個數n。
輸出
輸出兩個數aa和bb,如果不存在aa和bb滿足條件,那麼輸出 -1 -1。
輸出要求a和b爲正整數,
4
45 49
二、Solution
方法一:枚舉 ?
- 留下 0 分的淚水…😂
- 無論一個數,是質數還是合數,它的倍數,幾乎都是合數,但對於 1 而言,他是非合數。
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
static class Solution {
boolean isPrime(int n) {
for (int i = 2; i <= n / i; i++) {
if (n % i == 0)
return false;
}
return true;
}
void init() {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
long n = sc.nextLong();
for (int i = 1; ; i++)
for (int j = 1; ; j++) {
if (Math.abs(i - j) == n && !isPrime(i) && !isPrime(j)) {
System.out.println(i + " " + j);
return;
}
}
}
}
public static void main(String[] args) throws IOException {
Solution s = new Solution();
s.init();
}
}
方法二:思維
- 由 得,只需讓兩個數的係數爲相鄰的兩個整數即可求出兩個差爲 n 的合數。
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
static class Solution {
void init() {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
System.out.println(8*n + " " + 9 * n);
}
}
public static void main(String[] args) throws IOException {
Solution s = new Solution();
s.init();
}
}
複雜度分析
- 時間複雜度:,
- 空間複雜度:,