设计一个类,判断输入的数是否是水仙花数
谢了,速度解答,一定要正确
[最优解释]
class abc
{
public void ishuixian(int num)
{
int bai = 0;
int shi = 0;
int ge = 0;
int baiyushu = 0;
bai = num / 100;
baiyushu = num % 100;
shi = baiyushu / 10;
ge = baiyushu % 10;
if (num == bai * bai * bai + shi * shi * shi + ge * ge * ge)
{
Console.WriteLine(num + "是水仙花数");
}
else
{
Console.WriteLine(num + "不是水仙花数");
}
}
[其他解释]
int a = Convert.ToInt32(Console.ReadLine());
int a1 = a;
int j = a.ToString().Length;
double[] c = new double[j];
double b = 0;
for (int i = 0; i < j; i++)
{
c[i] = a % 10;
//Console.WriteLine(c[i]);
a = a / 10;
}
Console.WriteLine("==========================================");
foreach (double item in c)
{
b += System.Math.Pow(item,3);
//Console.WriteLine(System.Math.Pow(item, 3));
}
Console.WriteLine(b);
if ((int)b==a1 )
{
Console.WriteLine("TRUE");
}
else
{
Console.WriteLine("FALSE");
}