eg: 28=1+2+4+7+14
即一个数等于它所有公约数(除本身)之和
using System;
using System.Collections.Generic;
using System.Text;
namespace ExIsWanShu
{
class IsWanShu
{
public bool IsWanShu(int Num)
{
int s=0;
for (int i=1;i<Num;i++)
{
if((Num%i)==0)
s=s+i;
}
if (Num == s)
{
string str = s.ToString() + "=";
for (int i = 1; i < s; i++)
{
if ((s % i) == 0)
{
str = str +"+"+ i.ToString();
}
}
Console.Write(str+",");
return true;
}
return false;
}
static void Main(string[] args)
{
IsWanShu e = new IsWanShu();
int Num = Convert.ToInt32(Console.ReadLine());
bool b = e.IsWanShu(Num);
if (b)
{
Console.WriteLine("{0}是完数", Num);
}
else
{
Console.WriteLine("{0}不是完数", Num);
}
}
}
}