C语言教程:水仙花数
求出1000以内的水仙花数
首先知道水仙花数是什么:水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。 来源于百度百科。
例程:
#include <stdio.h>
#include <stdlib.h>
static void test(void)
{
int i;
int a,b,c;
for(i = 100;i < 1000;i++)
{
a = i/100;//主要取出百位的数值
b = i%100/10;//先利用百位数取余十位和个位,再整除取十位的数值
c = i%10;//主要取出个位的数值,直接除于十取余
if(i == a*a*a + b*b*b + c*c*c)
printf("%d\n",i);
}
}
int main()
{
test();
exit(0);
}
结果:
还没有评论,来说两句吧...