給一個數字 n
判斷他是不是完全數
輸出計算過程
如果是完全數,輸出{n} is perfect.
如果不是 且 有友好數,輸出{n} and {m} are friends.
如果不是 且 沒有友好數,輸出{n} has no friends.
評分結果(參考) : AC (2ms, 348KB)
#include<bits/stdc++.h>
#define int long long
using namespace std;
int getFriend(int num){
if(num == 1){
cout << "=0\n";
return 0;
}
int sum = 1;
set<int> st;
for(int i=2; i<=sqrt(num); i++){
if(num%i == 0){
st.insert(i);
st.insert(num/i);
}
}
cout << "1";
for(int i : st){
cout << "+" << i;
sum += i;
}
cout << "=" << sum << "\n";
return sum;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int x = getFriend(n);
if(x == n){
cout << n << " is perfect.";
}
else{
int y = getFriend(x);
if(y == n) cout << n << " and " << x << " are friends.";
else cout << n << " has no friends.";
}
return 0;
}