当前位置:网站首页>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 !
边栏推荐
- ROS知识点——消息过滤器 ( message_filters)
- Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
- em120.gige.h
- Idea2021.1 installation tutorial
- class和getClass()的区别
- 2022 interview questions
- Sword finger offer 22 The penultimate node in the linked list
- How to create a new page for SAP Spartacus storefront
- Eth data set download and related problems
- Vscode knowledge points - Common Errors
猜你喜欢

The construction of scalable distributed database cluster and the partition design of oneproxy sub database

This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W

ETH数据集下载及相关问题

Sword finger offer 26 Substructure of tree

Eth data set download and related problems

easyswoole3.2重启不成功

【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)

Alibaba Tianchi SQL learning notes - Day3

Microservice architecture practice: Construction of scalable distributed database cluster

Timing / counter of 32 and 51 single chip microcomputer
随机推荐
Meanings of SNAT, DNAT and masquerade in iptables
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
vector的底层模拟实现
Uniapp H5 page calls wechat payment
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
ROS knowledge point - message_filters
Sword finger offer 26 Substructure of tree
Helm kubernetes package management tool
TCP拥塞控制详解 | 2. 背景
si446使用记录(一):基本资料获取
em120.gige.h
Eye of depth (II) -- matrix and its basic operations
牛客 JS3 分隔符
[shutter] dart data type (dynamic data type)
[web technology] 1233 seconds understand web component
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
Five reasons to choose SAP Spartacus as the implementation framework of SAP commerce cloud storefront
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
Schoolbag novel multithreaded crawler [easy to understand]