你要是要代码 还是要结果啊
算了 代码结果都上了吧
class Program
{
static void Main(string[] args)
{
for (int iNum = 100; iNum <= 999; iNum++)
{
if (IsdaffodilNum(iNum))
{
Console.WriteLine("{0} 是水仙数",iNum);
}
}
Console.ReadKey();
}
/// <summary>
/// 判断是否是水仙数
/// </summary>
/// <param name="num">用户传入的参数</param>
/// <returns></returns>
public static bool IsdaffodilNum(int num)
{
try
{
int hundreds = 0;//百位数
int deckle = 0;//十位数
int unit = 0;//个位数
if (num >= 100 && num <= 999)
{
hundreds = num / 100;
deckle = (num/10) % 10;
unit = num % 10;
}
if (GetCube(hundreds) + GetCube(deckle) + GetCube(unit) == num)//如果符合水仙数的要求就返回true
{
return true;
}
else
{
return false;
}
}
catch
{
throw new Exception("输入的不是有效数字");
}
}
/// <summary>
/// 获取输入数的立方
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static int GetCube(int num)
{
return num * num * num;
}
}
|
|