//个位的立方加上十位的立方再加上百位的立方等于这个数本身,此数叫作水仙花数
for (int i = 100; i <= 999; i++)
{
int hundred = i / 100; //取得百位上的数
int decade = i / 10 % 10; //取得十位上的数
int unit = i % 10; //取得个位上的数
if ((hundred * hundred * hundred + decade * decade * decade + unit * unit * unit) == i)
{
Console.Write("水仙花{0,5}",i);
}
}
for (int hundred = 1; hundred <= 9; hundred++) //第一层for循环,循环百位数,百位数必须0<hundred<=9
{
for (int decade = 0; decade <= 9; decade++) //第二层for循环,循环十位数,十位数必须0<=decade<=9
{
for (int unit = 0; unit <= 9; unit++) //第三层for循环,循环个位数,个位数必须0<=unit<=9
{
if ((hundred * hundred * hundred + decade * decade * decade + unit * unit * unit) == (hundred * 100 + decade * 10 + unit))
{
Console.Write("水仙花{0,5}", hundred * 100 + decade * 10 + unit);
}
}
}
} |