我是靠谱客的博主 聪慧月饼,这篇文章主要介绍求水仙花数c语言代码怎么写,现在分享给大家,希望可以做个参考。

求水仙花数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("%dn",i); } return EXIT_SUCCESS; }
登录后复制

最后

以上就是聪慧月饼最近收集整理的关于求水仙花数c语言代码怎么写的全部内容,更多相关求水仙花数c语言代码怎么写内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(138)

评论列表共有 0 条评论

立即
投稿
返回
顶部