2 weixin 28772505 weixin_28772505 于 2015.06.20 21:35 提问

这是求水仙花数的代码,请大神指出错误

程序运行得不出结果图片

2个回答

u010189941
u010189941   2015.06.20 23:26

#include
void main()
{
int i,a,b,c;
printf("水仙花数:\n");
for(i=100;i<=999;i++)
{ a=i/100 ;
b=(i-100*a)/10 ;
c=i-100*a-10*b;
if(i==a*a*a+b*b*b+c*c*c)
printf("%d\n",i);
}
}

u010189941
u010189941 #include<stdio.h>
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.20 21:37
 #include <stdio.h>
#include <math.h>

const char * is_narcissistic_p(int n) {
    int sum_cubic = 0, m;
    for (m = n; m > 0; m /= 10) {
        sum_cubic += (int) pow(m % 10, 3);
    }
    return sum_cubic == n ? "yes" : "no";
}

int main() {
    int n[100] = {0}, i, j;
    printf("Which numbers do you want to check for narcissisticibility?\n");
    for (scanf("%d", &i), j = 0; i != 0; scanf("%d", &i), j++) {
        n[j] = i;
    }
    for (i = 0; i < j; i++) {
        printf("%s\n", is_narcissistic_p(n[i]));
    }
    return 0;
}
Csdn user default icon
上传中...
上传图片
插入图片