题解 | 水仙花数
水仙花数
https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703
#include <stdio.h>
#include "math.h"
int shui(int n){
int weight=0;
int t=n;
while(t>0){
weight++;
t/=10;
}
t=n;
int sum=0;
while(t>0){
sum+=pow(t%10,weight);
t/=10;
}
if(sum==n){
return 1;
}else{
return 0;
}
}
int main() {
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
int sign=0;
for(int i=n;i<=m;i++){
if(shui(i)){
sign=1;
printf("%d ",i);
}
}
if(!sign){
printf("no");
}
printf("\n");
}
return 0;
}
查看10道真题和解析