题解 | #水仙花数#
水仙花数
https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703
大一新生
#include <stdio.h>
int main() {
int a,b;
int i;
while (scanf("%d %d",&a,&b) != EOF)
{
getchar();//换行不运行
int count=0;
for(i=a;i<=b;i++)
{
int p,s,g,sum;
p=i/100;//百位数,如153/100=1
s=(i%100)/10;//十位数,如153%100=53,53/10=5
g=(i%100)%10;//个位数,如153%100=53,53%10=3
sum=(p*p*p)+(s*s*s)+(g*g*g);//各位数字立方和
if (sum==i)
{
printf("%d ",i);//输出水仙花数
count++;//记录水仙花数
}
}
if(count==0)
{
printf("no\n");//无水仙花数时
}
}
return 0;
}
网易游戏公司福利 566人发布
查看19道真题和解析