思维挑战9:水仙花数

思维挑战9:水仙花数

水仙花数:即三位数的每个位置的立方的和等于这个数。
那么我们如何找出这些水仙花数呢?

//C033拼接法
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,j,k;
    for(i=1;i<=9;i++)
        for(j=0;j<=9;j++)
            for(k=0;k<=9;k++)
                if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
                    printf("%d\n",i*100+j*10+k);
    system("pause");
    return 0;
}


//C034
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,c,x;
    for(x=100;x<=999;x++)
    {
        a=x/100;
        b=x/10%10;
        c=x%10;
        if(a*a*a+b*b*b+c*c*c==x)
        printf("%d\n",x);
    }
    system("pause");
    return 0;
}
发布了33 篇原创文章 · 获赞 2 · 访问量 961

猜你喜欢

转载自blog.csdn.net/Btbsja/article/details/104312192