本文摘自PHP中文网,作者angryTom,侵删。

求水仙花数c语言代码怎么写
水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
推荐学习:c语言视频教程
下面是使用C语言求水仙花数的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # include <stdio.h>
# include <stdlib.h>
void main()
{
int i,j,k,n;
printf( "'water flower'number is:" );
for (n=100;n<1000;n++)
{
i=n/100;
j=n/10%10;
k=n%10;
if (n==i*i*i+j*j*j+k*k*k)
{
printf( "%-5d" ,n);
}
}
printf( "\n" );
}
|
升级版:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
int cube( const int n){
return n*n*n;
}
bool
isNarcissistic( const int n){
int hundreds=n/100;
int tens=n/10-hundreds*10;
int ones=n%10;
return cube(hundreds)+cube(tens)+cube(ones)==n;
}
int main(void){
int i;
for (i=100;i<1000;++i){
if (isNarcissistic(i))
printf( "%d\n" ,i);
}
return EXIT_SUCCESS;
}
|
更多C语言教程,请关注PHP中文网!
以上就是求水仙花数c语言代码怎么写的详细内容!
相关阅读 >>
c语言六种基本语句是什么
c语言是面向什么的语言
c语言函数声明格式是什么?
c语言中数组的下标从什么开始?
c语言中1e-6什么意思
c语言sqrt函数的用法
c语言中switch语句的case后能否是一个关系表达式
c语言编写strcpy函数的方法
c语言中strstr函数的用法是什么?
c语言中的函数可不可以单独进行编译?
更多相关阅读请进入《水仙花数》频道 >>
转载请注明出处:木庄网络博客 » 求水仙花数c语言代码怎么写