水仙花数

打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数

本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 水仙花算法 {
 7     class Program {
 8         static void Main ( string[ ] args ) {
 9             int start = 100;
10             int end =1000;
11             while ( start<end ) {
12                 start++;
13                 string str = start.ToString ( );
14                 int x = Convert.ToInt32 ( str[0].ToString ( ) );
15                 int y = Convert.ToInt32 ( str[1].ToString ( ) );
16                 int z = Convert.ToInt32 ( str[2].ToString ( ) );
17                 if ( ( Math.Pow ( x, 3 ) + Math.Pow ( y, 3 ) + Math.Pow ( z, 3 ) ) == start ) {
18                     Console.WriteLine ( start );
19                 }
20             }
21             Console.ReadKey ( );
22         }
23     }
24 }
posted @ 2012-06-29 00:39  张小三、  阅读(175)  评论(0编辑  收藏  举报