描述
请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。
输入
有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)
输入0表示程序输入结束。
输出
如果n是水仙花数就输出Yes
否则输出No
样例输入
153
154
0
样例输出
Yes
No
请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。
输入
有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)
输入0表示程序输入结束。
输出
如果n是水仙花数就输出Yes
否则输出No
样例输入
153
154
0
样例输出
Yes
No
代码:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
do
{
int readNum = 0;
scanf("%d",&readNum);
getchar();
if(readNum < 100 || readNum >= 1000)
{
break;
}
int tmp = readNum;
a = tmp/100;
tmp = tmp%100;
b = tmp/10;
c = tmp%10;
if( a*a*a + b*b*b + c*c*c == readNum)
{
printf("Yes ");
}
else
{
printf("No ");
}
}while(1);
return 0;
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
do
{
int readNum = 0;
scanf("%d",&readNum);
getchar();
if(readNum < 100 || readNum >= 1000)
{
break;
}
int tmp = readNum;
a = tmp/100;
tmp = tmp%100;
b = tmp/10;
c = tmp%10;
if( a*a*a + b*b*b + c*c*c == readNum)
{
printf("Yes ");
}
else
{
printf("No ");
}
}while(1);
return 0;
}
推荐的优秀代码:
#include<iostream>
using namespace std;
int main()
{
int a;
while(1)
{
cin>>a;
if(a==0) break;
cout<<((a==153||a==370||a==371||a==407)?"Yes":"No")<<endl;
}
}
虽然。。。但是。。。
偷懒的思想还是需要的,毕竟程序员应该追求的是简约,而不是高深,但是作为入门的练手题,考虑如何分解一个整数的各个位数,还是需要掌握的。