求100-999之间的水仙花数

来源:互联网 发布:手机如何注销淘宝店铺 编辑:程序博客网 时间:2024/05/01 13:11

"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:  153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 水仙花数{    class Program    {        static void Main(string[] args)        {            for (int i = 100; i < 999; i++)            {                if (isTrue(i))                {                    Console.WriteLine("水仙花数:" + i.ToString());                }            }            Console.WriteLine("结束");            Console.ReadKey();        }        private static bool isTrue(int i)        {            //取个位            int a = i / 100;            int b = (i - a * 100) / 10;            int c = i - a * 100 - b * 10;            if (a*a*a+b*b*b+c*c*c==i)            {                return true;            }                        return false;        }    }}


0 0
原创粉丝点击