0
点赞
收藏
分享

微信扫一扫

1116 水仙花数


Description
如果一个n位数的每个数位的n次方和就是本身,那么我们称这种数为“水仙花数”。比如371,33+73+13=27+343+1 = 371。现给你一个数,请求这个数是否是水仙花数。

输入

     有多组样例。每个样例占一行,为一个整数a(1<=a <=1,000,000),如果a为 0,那么表示输入结束,这个样例不需要处理。

输出

     每行输出一个样例的结果,如果是就输出“Yes”,否则输出“No”。

Sample Input
1
370
371
600000
700000
0

Sample Output
Yes
Yes
Yes
No
No

Source

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char a[20];//定义一个字符串,其包含20个字符
while(scanf("%s",&a))//数字以字符串的形式存储
{
if(a[0]=='0')//输入为0时,直接结束循环
break;
int n;//定义n
n=strlen(a);//n为长度数值。strlen()函数作用:获取字符串真实长度。头文件为#include <string.h>
int x=atoi(a);//atoi()函数作用:该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。头文件为<stdlib.h>
int i,m=0,s;
for(i=0;i<=n-1;i++)//进行相加计算
{
s=a[i]-48;//ascii码的性质。字符串0到字符串9的ascii值为48~57.字符串减去48就是相应的十进制数
m+=pow(s,n);//即是m=m+pow(s,n)。
}
if(m==x)//判断
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}

写于2021年8月9日20:45。
多敲几遍。最重要的是独立思考的过程。


举报

相关推荐

水仙花数(java)

打印水仙花数

JAVA——水仙花数

c水仙花数

计算“水仙花数”

求水仙花数

0 条评论