水仙花数问题
#include<stdio.h>int main(){
int m,n;
int e=0;
while(scanf("%d %d",&m,&n)!=-1){
int i;
for(i=m;i<=n;i++){
int a,b,c,d;
a=i/100;//百位上的数
b=i%100;//去掉百位
c=b/10;//十位上
d=b%10;//个位上
if(i==(a*a*a+b*b*b+c*c*c)){
printf("%d",i);
e++;}
}
if(e==0){printf("no\n");}
else{printf("\n");}
}
return 0;
}
我从100到999只能打出来407这一个水仙花数,不知道哪里错了 本帖最后由 sunrise085 于 2020-9-11 16:16 编辑
你的程序第13行有问题啊
abc并不是百位、十位和个位啊,你的十位是c,个位是d
#include<stdio.h>
int main(){
int m,n;
int e=0;
while(scanf("%d %d",&m,&n)!=-1){
int i;
for(i=m;i<=n;i++){
int a,b,c,d;
a=i/100;//百位上的数
b=i%100;//去掉百位
c=b/10;//十位上
d=b%10;//个位上
if(i==(a*a*a+c*c*c+d*d*d)){//这里搞错啦,b不是十位,c才是十位,d是个位
printf("%d ",i);
e++;}
}
if(e==0){printf("no\n");}
else{printf("\n");}
}
return 0;
} sunrise085 发表于 2020-9-11 16:14
你的程序第13行有问题啊
abc并不是百位、十位和个位啊,你的十位是c,个位是d
是这样的 sunrise085 发表于 2020-9-11 16:14
你的程序第13行有问题啊
abc并不是百位、十位和个位啊,你的十位是c,个位是d
我脑瘫了,不好意思,麻烦你了
页:
[1]