日期:2014-05-17  浏览次数:20778 次

c# 水仙花数 两种方法

方法1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k;
            for (i = 0; i < 10; i++)
                for (j = 0; j < 10; j++)
                    for (k = 0; k < 10; k++)
                        if (i * i * i + j * j * j + k * k * k == i + 10 * j + 100 * k)
                            Console.WriteLine(i * i * i + j * j * j + k * k * k);
            Console.ReadKey();
        }
    }
}


方法2:

using System;
using System.Collections.Generic;
using System.Text;
namespace test
{
   class Program
   {
      static void Main(string[] args)
      {
         int ge, shi, bai;
         Console.WriteLine("所有水仙花数是:");
         for (int i = 100; i <= 999; i++)
         {
            ge = i % 10;
            shi = i / 10 % 10;
            bai = i / 100;
            if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
               Console.Write("  {0}", i);
         }
         Console.Read();
      }
   }
}