当前位置:网站首页>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 !
边栏推荐
- The difference of message mechanism between MFC and QT
- 牛客JS2 文件扩展名
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
- SSB threshold_ SSB modulation "suggestions collection"
- Timing / counter of 32 and 51 single chip microcomputer
- 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
- chrome瀏覽器快速訪問stackoverflow
- 每日一题——“水仙花数”
- When the industrial Internet began to enter the deep-water area, it appeared more in the form of industry
猜你喜欢
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
Simple linear programming problem
What if the default browser cannot be set?
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
Map集合详细讲解
The bottom simulation implementation of vector
Sword finger offer 27 Image of binary tree
Sword finger offer 26 Substructure of tree
MATLAB中nexttile函数使用
The difference of message mechanism between MFC and QT
随机推荐
What if the default browser cannot be set?
泡椒凤爪制作教程
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
【网络是怎样连接的】第五章 探索服务器
ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
The beginning of life
ROS knowledge point - message_filters
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
PCL知识点——体素化网格方法对点云进行下采样
[web technology] 1233 seconds understand web component
List summation [dummy+ tail interpolation + function processing list reference common pit]
Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
Simple linear programming problem
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)
牛客 JS3 分隔符
书包网小说多线程爬虫[通俗易懂]
One year is worth ten years
executescalar mysql_ExecuteScalar()