真水无香No.1

导航

 

题目:编写一个控制台应用程序,求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();
        }
    }

  

posted on 2013-04-14 14:25  真水无香No.1  阅读(150)  评论(0)    收藏  举报