Description
古希臘數學家畢達哥拉斯在自然數研究中發現,220的所有真約數(即不是自身的約數)之和為:
1+2+4+5+10+11+20+22+44+55+110=284。
而284的所有真約數為1、2、4、71、 142,加起來恰好為220。人們對這樣的數感到很驚奇,并稱之為親和數。一般地講,如果兩個數中任何一個數都是另一個數的真約數之和,則這兩個數就是親和數。
你的任務就撰寫一個程式,判斷給定的兩個數是否是親和數
Input
輸入資料第一行包含一個數M,接下有M行,每行一個實體,包含兩個整數A,B; 其中 0 <=A,B <=600000 ;
Output
對于每個測驗實體,如果A和B是親和數的話輸出YES,否則輸出NO。
Sample Input
2
220 284
100 200
Sample Output
YES
NO
代碼:
#include<bits/stdc++.h>
using namespace std;
int a,b,m,sum;
int judge(int s)
{
sum=0;
for(int j=1;j<=s/2;j++)
{
if(s%j==0)
sum=sum+j;
}
return sum;
}
int main()
{
cin>>m;
for(int i=0;i<m;i++)
{
int x,y;
cin>>a>>b;
x=judge(a);
y=judge(b);
if(x==b&&y==a)
printf("YES\n");
else
printf("NO\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/237346.html
標籤:新手樂園
上一篇:c語言檔案的疑問
