Problem Description
古希臘數(shù)學(xué)家畢達(dá)哥拉斯在自然數(shù)研究中發(fā)現(xiàn),220的所有真約數(shù)(即不是自身的約數(shù))之和為:
1+2+4+5+10+11+20+22+44+55+110=284。
而284的所有真約數(shù)為1、2、4、71、 142,加起來恰好為220。人們對這樣的數(shù)感到很驚奇,并稱之為親和數(shù)。一般地講,如果兩個(gè)數(shù)中任何一個(gè)數(shù)都是另一個(gè)數(shù)的真約數(shù)之和,則這兩個(gè)數(shù)就是親和數(shù)。
你的任務(wù)就編寫一個(gè)程序,判斷給定的兩個(gè)數(shù)是否是親和數(shù)
Input
輸入數(shù)據(jù)第一行包含一個(gè)數(shù)M,接下有M行,每行一個(gè)實(shí)例,包含兩個(gè)整數(shù)A,B; 其中 0 <= A,B <= 600000 ;
Output
對于每個(gè)測試實(shí)例,如果A和B是親和數(shù)的話輸出YES,否則輸出NO。
Sample Input
2 220 284 100 200
Sample Output
YES NO
Author
linle
java code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
int a = input.nextInt();
int b = input.nextInt();
if ((getQinHe(a) == b) && (getQinHe(b) == a)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
input.close();
}
public static int getQinHe(int k) {
int sum = 0;
for (int i = 1; i <= k / 2; i++) {
if (k % i == 0) {
sum += i;
}
}
return sum;
}
}```