题目:编写一个控制台应用程序,求1000之内的所有“完数”!
class Program
{
static void Main(string[] args)
{
for (int i = 2; i <= 1000; i++)
{
int s = 1;
string str = "1";
for (int j = 2; j <= (int)Math.Sqrt(i); j++)
{
if (j * (i / j) == i)
{
if (j != i / j)
{
s += j + i / j;
str += string.Format("+{0}+{1}", j, i / j);
}
else
{
s += j;
str += string.Format("+{0}", j);
}
}
}
if (s == i) Console.WriteLine("{0}={1}", i, str);
}
Console.ReadLine();
}
}
浙公网安备 33010602011771号