• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

求水仙花数c语言代码怎么写

c语言 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

求水仙花数c语言代码怎么写

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

推荐学习:c语言视频教程

下面是使用C语言求水仙花数的代码:

#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;/*分解出百位*/  <strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码网^</strong>       j=n/10%10;/*分解出十位*/         k=n%10;/*分解出个位*/         if(n==i*i*i+j*j*j+k*k*k)         {             printf("%-5d",n);         }     }     printf("\n"); }

升级版:

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>int cube(const int n){    return n*n*n;}boolisNarcissistic(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语言教程,请关注搞代码

以上就是求水仙花数c语言代码怎么写的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求水仙花数c语言代码怎么写
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址