水仙花數指三位數,其各位數字立方和等於該數本身。例如 153 = 1³ + 5³ + 3³。
using System;
class Program {
static void Main() {
Console.WriteLine("100-999 之間的水仙花數:");
for (int i = 100; i < 1000; i++) {
int a = i / 100;
int b = (i / 10) % 10;
int c = i % 10;
if (a*a*a + b*b*b + c*c*c == i) {
Console.Write(i + " ");
}
}
Console.WriteLine();
}
}
153 370 371 407
Math.Pow 或預計算 0-9 的立方表可提升效能。