自娱自乐的小题目(3)

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

这个题目我发现我写的有点2= =

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

namespace Test3 {
    class Program {
        //题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
        //153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
        static void Main(string[] args) {
            int a, b, c, e, d, f, sum;
            for (int i = 100; i < 1000; i++) {
                a = (int)i / 100;
                b = (int)(i - a * 100) / 10;
                c = (int)(i - a * 100 - b * 10);
                d = (int)Math.Pow(a, 3);
                e = (int)Math.Pow(b, 3);
                f = (int)Math.Pow(c, 3);
                sum = d + e + f;
                if (d + e + f == i) {
                    Console.WriteLine(i);
                }
            }
        }
    }
}

 

 

posted @ 2012-05-26 12:48  世界很灰暗  阅读(148)  评论(0编辑  收藏  举报