咋样判断随意输入一个三到五位的数是否为水仙数,
首先判断是几位数。然后取出每一位计算几次方的和是否等于这个数本身
#include <stdio.h>
#include <math.h>
int main()
{
int n,b,t=0,sum=0;
scanf("%d",&n);
b=n;
while(b>0)
{
t++;
b=b/10;
}
b=n;
while(b>0)
{
sum += pow(b%10,t);
b=b/10;
}
if(sum == n)
printf("%d是水仙花数",n);
else
printf("%d不是水仙花数",n);
return 0;
}
#include<stdio.h>
#include<math.h>
int main() {
int n,t,s=0;
scanf("%d",&n);
t=n;
while(t!=0){
s+=pow(t%10,3);
t/=10;
}
if(s==n){
printf("Yes");
}else{
printf("No");
}
return 0;
}
觉得有用的话采纳一下哈
水仙花数是三位数,应该是自幂数。