当前位置:网站首页>Daily question - "number of daffodils"
Daily question - "number of daffodils"
2022-07-02 17:39:00 【Protect Xiaozhou】
Hello, everyone , I'm protecting Xiao Zhou ღ, What this issue brings to you is seeking “ Narcissistic number ”, This daffodil , Not that daffodil , Let's have a look ~

subject :
Find out 0~100000 Between all “ Narcissistic number ” And the output .
describe :
“ Narcissistic number ” It means a n digit , Of its digits n The sum of the powers is just equal to the number itself , Such as :153=1^3+5^3+3^3, be 153 It's a “ Narcissistic number ”.
The scope of attention is 0~100000 Between all that match the description “ Narcissistic number ”.
Thinking analysis :
Based on the description , We know , Your numbers are n Only when the sum of the powers is just equal to the number itself can it be called “ Narcissistic number ”, First try to judge whether a number is daffodil number , How to judge ?
First step : Find the number of digits n.
The second step : Find the n The sum of the powers .
The third step : Judge the number n Whether the sum of the powers is just equal to the number itself
This blogger brings you two ways to solve problems , It's all the same , Ordinary counting , Recursive count .
Ordinary counting :
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// Count the number of digits of each number
int Count(int size)
{
int n = 0;
while (size)
{
size = size / 10;
++n;
}
return n;
}
// Judge whether it is daffodil
int IfDaffodil(int i,int n)
{
int size = i;
int sum = 0;
// Judge whether it meets “ daffodils ”
while (size!=0)
{
int ssum = 1;
//ssum Count each person's n Power
for (int j = 0; j < n; j++)
{
ssum *= size % 10;
}
size = size / 10;
//sum Statistics for everyone n The sum of the powers
sum += ssum;
}
// Your numbers are n The sum of the powers is exactly equal to the number itself
if (sum == i)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
// Ergodic interval
for (int i = 0; i <= 100000; ++i)
{
// When the program is executed IfDaffodil when , Will first perform as “ Parameters ” Of Count, etc. Count Execute yourself after execution
int size=IfDaffodil(i, Count(i));
if (size == 1)
{
printf("%d ",i);
}
else
{
continue;
}
}
return 0;
}
Recursive count :
#include<stdio.h>
// Count the number of digits of each number
int Count(int n)
{
if (n < 10)// When there is only one digit left in the number , Recursion ends
return 1;
else
return Count(n / 10) + 1;
/*
{
n=n/10;
return Count(n)+1;
}
*/
}
// Calculate the number of each digit n Power
int Pow(int x, int n)
{
if (n == 0)
return 1;
else
return pow(x, --n) * x;//x^n
}
int main()
{
// Ergodic interval
for (int i = 0; i < 100000; ++i)
{
int n = i;
int sum = 0;
// Judge whether it meets “ daffodils ”
while (n)
{
//n % 10 Get the last one , Then find the power according to its own digits
sum += (Pow((n % 10), Count(i)));//Pow Calculate the number of each digit n Power
n /= 10;
}
if (sum == i)
{
printf("%d ", sum);
}
else
{
continue;
}
}
return 0;
}
Interested friends can use the blogger's method , Or do this problem in your own way .
Share a similar topic on Niuke website , You can also try to do it .
link : Narcissistic number _ Niuke Tiba _ Cattle from
Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ **,°*:.*( ̄▽ ̄)/$:*.°**

If there is infringement, please contact to modify and delete !
边栏推荐
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
- Making tutorial of chicken feet with pickled peppers
- Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
- PCL知识点——体素化网格方法对点云进行下采样
- Sword finger offer 27 Image of binary tree
- Ssm+ wechat applet to realize property management system
- Eye of depth (III) -- determinant of matrix
- OpenHarmony如何启动FA(本地和远程)
- SAP commerce cloud storefront framework selection: accelerator or Spartacus?
- SAP commerce Cloud Architecture Overview
猜你喜欢
随机推荐
ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
class和getClass()的区别
Microservice architecture practice: Construction of scalable distributed database cluster
Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
easyswoole3.2重启不成功
Blog theme "text" summer fresh Special Edition
IDEA2021.1 安装教程
Sword finger offer 24 Reverse linked list
常用SQL语句(完整范例)
One year is worth ten years
Ssm+ wechat applet to realize property management system
VirtualLab基础实验教程-7.偏振(2)
Dstat use [easy to understand]
Helm kubernetes package management tool
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
ROS knowledge point - message_filters
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
牛客 JS3 分隔符
chmod命令原理及用法详解[通俗易懂]
From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)








