【C#】輸出一個範圍內所有的水仙花數

// Copyright (c) 2013, 煙臺大學計算機學院
// All rights reserved.
// 作    者:  沈遠宏
// 完成日期:2014 年 09月04日
// 版 本 號:v1.0
//問題描述:輸出一個範圍內所有的水仙花數
//所謂水仙花數是一個三位整數,其各位數字的立方和等於該數本身。
//例如:153=1*1*1+5*5*5+3*3*3
// 輸出:水仙花數
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1-1000的所有水仙花數:");
            for (int i = 100; i <= 1000; i++)
            {
                int ge, shi, bai;
                ge = i % 10;
                shi = i /10 % 10;
                bai = i / 100;
                if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i)
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadLine();
        }
    }
}


運行結果:

心得體會:

就當拿這些小程序回顧一下大一的知識

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章