c184.
盈虧互補
--
板橋高中教學題
| From: [36.235.69.111] |
發表日期
:
2024-04-10 00:03
想法:定義一個計算[真因數]的函數swi(n),計算出真因數總和後,再丟入swi( )計算是否為完全數
#include<bits/stdc++.h>
using namespace std;
int swi(int n)
{
vector<int> list;
list.push_back(1);
for(int i=2;i<int(pow(n,0.5))+1;i++)
{
if(n%i==0)
{
list.push_back(i);
list.push_back(n/i);
}
}
if(int(pow(list.back(),2))==n) //拿掉重複的因數 ex:36=6*6
{
list.pop_back();
}
sort(list.begin(),list.end());
int sum=0;
for(int i=0;i<list.size();i++)
{
sum+=list[i];
}
if(!list.empty())
{
cout<<list[0];
}
for(int i=1;i<list.size();i++)
{
cout<<"+"<<list[i];
}
cout<<"="<<sum<<endl;
return sum;
}
int main()
{
int n;
while(cin>>n)
{
int sum=0;
sum=swi(n);
if(n==sum)
{
cout<<sum<<" is perfect."<<endl;
}
else if(swi(sum)==n)
{
cout<<n<<" and "<<sum<<" are friends."<<endl;
}
else
{
cout<<n<<" has no friends."<<endl;//對。你沒朋友
}
}
}