当前位置:网站首页>Calculating the number of daffodils in C language
Calculating the number of daffodils in C language
2022-07-05 23:05:00 【Green abundance is not green】
“ Narcissistic number ” It means a n digit , Of its digits n The sum of the powers is exactly equal to the number itself , Such as :153=1^3+5^3+3^3, be 153 It's a “ Narcissistic number ”.
Ideas :
- Find out how many digits this number is , Such as 153 It's a three digit number
- Get every number that makes up this number , Such as 153 Medium 1、5、3
- Put a single number of n The power is stored in the array , Such as sum[1]=1^3,sum[2]=5^3,sum[3]=3^3
- Add all the elements of the array ,all=sum[1]+sum[2]+sum[3]
- Judge whether the sum is equal to this number , Equal is the number of daffodils
#include<stdio.h>
int narcissus(int n) // Determine whether the number of Narcissus
{
int num = 1; //n Number of digits
int count = 1;
int single = 0; // Variable , Store n A single number in
int sum[7] = {}; // Used to store a single number n Power
int all = 0; // Array sum Add all values of
int mun = num; // take num Assign a value to mun, Such changes mun Will not change the original num Value
int m = n; // take n The value is assigned to m, Such changes m When n Will not be changed
while ((n / count)>1) // Judge n How many digits is it
{
count *= 10;
num++;
}
while (mun >= 0) //n Is a few digits , Just cycle a few times , Find the num Power
{
single = m % 10;
sum[mun] = single;
for (int i=0; i <= num; i++)
{
sum[mun] *= single; // Find the num Power , And store it sum Array
}
m /= 10; // take n Right shift to position , Such as 123->12
mun--;
}
for (int i = 0; i <= 6; i++)
{
all += sum[i]; // Calculation sum Array and
}
if (all == n)
{
return n; // If it's narcissus number , Then return to n, If not, return to 0
}
return 0;
}
int main()
{
int n = 0;
int i = 1;
while (i <= 100000) // from 1 To 100000 Number of daffodils in
{
int ret;
ret = narcissus(i); // Get the return value
if (ret != 0) // The return value is not 0 Then print
{
printf("%d ", ret);
}
i++;
}
return 0;
}
边栏推荐
- Methods modified by static
- 一文搞定class的微观结构和指令
- Hcip day 12 (BGP black hole, anti ring, configuration)
- 媒体查询:引入资源
- 数据库基础知识(面试)
- Shell: operator
- 两数之和、三数之和(排序+双指针)
- The method and principle of viewing the last modification time of the web page
- 透彻理解JVM类加载子系统
- [speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
猜你喜欢
The difference between MVVM and MVC
Use of grpc interceptor
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Codeforces Global Round 19
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
Debian 10 installation configuration
Tensor attribute statistics
Getting started stm32--gpio (running lantern) (nanny level)
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
随机推荐
Use of metadata in golang grpc
Boring boring
Composition of interface
媒体查询:引入资源
C Primer Plus Chapter 9 question 10 binary conversion
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
Starting from 1.5, build a micro Service Framework -- log tracking traceid
两数之和、三数之和(排序+双指针)
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Finally understand what dynamic planning is
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
I closed the open source project alinesno cloud service
fibonacci search
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
Vision Transformer (ViT)
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
TCC of distributed solutions
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)