当前位置:网站首页>C语言输出所有水仙花数
C语言输出所有水仙花数
2022-07-22 02:31:00 【Serendipity_-】
水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)
所谓水仙花数,就是一个三位数,它的 百位立方+十位立方+个位立方 = 原数。
例如,153是一个水仙花数,因为153=1³+5³+3³
主要使用for循环来实现:
#include <stdio.h>
int main(int argc, char const *argv[])
{
for (int i = 100; i < 1000; i++)
{
int x = i % 10;
int y = i / 10 % 10;
int z = i / 100;
if (i == x * x * x + y * y * y + z * z * z)
printf("%d ", i);
}
return 0;
}
或者:
#include <stdio.h>
int main(int argc, char const *argv[])
{
for (int i = 1; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
if (i * i * i + j * j * j + k * k * k == i * 100 + j * 10 + k)
printf("%d ", i * 100 + j * 10 + k);
}
}
}
return 0;
}
运行结果如下:
边栏推荐
猜你喜欢
码蹄集 - MT3182 - 填矩阵
AutoComplete(自动完成)
欢乐的彝族火把节Joyous Torch Festival of the Yi Nationality
ADB 命令结合 monkey 的简单使用,超详细
[07] function call: why does stack overflow happen?
nacos基础概念和单机启动
Chapter 3 - creating and maintaining MySQL data tables
Cookies and sessions
LeetCode 0814. 二叉树剪枝
Cloud native ide: the first general and powerful codeless development platform of IVX
随机推荐
【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
pyside2做个简易的浏览器
第三章第五节:返回值
[activity registration] stack a buff for your code! Click "tea" to receive the gift
产业数字化全面加速,智能云网2.0重新定义数字化转型
Code shoe set - mt2201 · sum of all
[Huawei machine test questions] maximum number of components [2022 Q3 | 100 points]
蒙特卡洛树搜索(MCTS)详解
ES6——模块
[07] function call: why does stack overflow happen?
Product banner style type analysis
Kubernetes基础部分学习笔记
卷妹的面试小抄每日更新Day1
ADB common commands
Codeworks 5 questions per day (average 1500) - day 22
调试VBS visual studio
Machine learning notes - overview of machine learning system design process
AutoLabel (AutoLabel)
codeforces每日5题(均1500)-第二十二天
Add, delete, check and modify MySQL (second sentence)