C语言:求出0~999之间的所有“水仙花数”并输出


    “水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3,则153是一个“水仙花数”。

    在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
    例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。
370 = 3^3 + 7^3 + 0^3。
371 = 3^3 + 7^3 + 1^3。
407 = 4^3 + 0^3 + 7^3。

#include<stdio.h>
#include<stdlib.h>
void NarcissisticNumber()
{
    int num = 0;
    int hundred = 0;
    int ten = 0;
    int unit = 0;

    for (; num < 1000; num++)
    {
        hundred = num / 100;
        ten = (num - hundred * 100) / 10;
        unit = (num - hundred * 100 - ten * 10) / 1;
        if (num == hundred * hundred * hundred
            + ten * ten * ten
            + unit *unit *unit)
        {
            printf("%d    ", num);
        }
    }
    printf("\n");
}

int main()
{
    NarcissisticNumber();
    system("pause");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值