当前位置:网站首页>How to find the number of daffodils with simple and rough methods in C language
How to find the number of daffodils with simple and rough methods in C language
2022-07-06 10:31:00 【Yisu cloud】
C How to count daffodils in a simple and rough way in language
This article mainly introduces C How to find the relevant knowledge of daffodil number in a simple and rough way in language , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article C How to use simple and crude methods to find daffodils in language and count articles will be fruitful , Let's have a look .
What is narcissus number :
Means a n digit , Of its digits n The sum of the powers is exactly equal to the number itself
for example :
1 1^1=1;
153 3^3+5^3+1^3=153;
problem : seek 0~100000 Between the number of daffodils , And print it out
Look at the topic , Find a breakthrough :
0~100000 I can think of using cycles to judge 0~10000 Between the digital
From chestnuts : We need to find out how many digits the number is
To use the power , Then we need to quote math In the library pow function
Sum up , Still use the loop
So start writing code , Step by step, think by step :
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
for (i = 0; i <= 100000; i++)
{
int n = 1; // n Is the number of digits
int z = i; // after while after i change ,i Assign a value to z, Replace original value i Carry out the following calculation
while (z / 10 != 0) // It's just beginning to use i Carry out operations Dead cycle
{
n++;
z=z / 10; // use i Operation , When i=10 when i=i/10 Directly equal to 1 , Dead cycle , So we use variables z Instead of i
}
if (i == Sum(i, n)) // Judge whether the sum is equal to the original value
printf("%d ",i);
}
return 0;
}Look at the code and be sure to look at the comments later , Here are some points to pay attention to :
It was used n Represents the number of digits , seek n when while The loop changes i value , It is not conducive to the following calculation , So use z Instead of
Set function Sum To calculate and , The function code is as follows :
int Sum(int x, int y) //x receive i,y receive n
{
int k = 0;
int num = 0;
int sum = 0;
for (k=0;k<y;k++) // Circle sum
{
num = pow(x % 10, y); // Take out the number of each digit , Find the power
sum += num; // Sum by accumulation
x/=10; // Remove the lowest number
}
return sum; // Return and
}The complete code is as follows :
#include <stdio.h>
#include <math.h>
int Sum(int x, int y)
{
int k = 0;
int num = 0;
int sum = 0;
for (k=0;k<y;k++)
{
num = pow(x % 10, y);
sum += num;
x/=10;
}
return sum;
}
int main()
{
int i = 0;
for (i = 0; i <= 100000; i++)
{
int n = 1;
int z = i;
while (z / 10 != 0)
{
n++;
z=z / 10;
}
if (i == Sum(i, n))
printf("%d ",i);
}
return 0;
}Running results :

About “C How to count daffodils in a simple and rough way in language ” That's all for this article , Thank you for reading ! I'm sure you're right “C How to count daffodils in a simple and rough way in language ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .
边栏推荐
- Notes of Dr. Carolyn ROS é's social networking speech
- text 文本数据增强方法 data argumentation
- 再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
- 软件测试工程师发展规划路线
- In fact, the implementation of current limiting is not complicated
- Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
- MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?
- Super detailed steps for pushing wechat official account H5 messages
- Record the first JDBC
- Security design verification of API interface: ticket, signature, timestamp
猜你喜欢

MySQL27-索引優化與查詢優化

Mysql30 transaction Basics

16 医疗挂号系统_【预约下单】

软件测试工程师必备之软技能:结构化思维

C miscellaneous two-way circular linked list

Southwest University: Hu hang - Analysis on learning behavior and learning effect

如何搭建接口自动化测试框架?

Implement context manager through with

Complete web login process through filter

MySQL底层的逻辑架构
随机推荐
Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
Introduction tutorial of typescript (dark horse programmer of station B)
Mysql35 master slave replication
百度百科数据爬取及内容分类识别
Emotional classification of 1.6 million comments on LSTM based on pytoch
保姆级手把手教你用C语言写三子棋
16 medical registration system_ [order by appointment]
Set shell script execution error to exit automatically
基于Pytorch的LSTM实战160万条评论情感分类
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
MySQL27-索引优化与查询优化
Software test engineer development planning route
Use xtrabackup for MySQL database physical backup
颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
简单解决phpjm加密问题 免费phpjm解密工具
pytorch的Dataset的使用
Adaptive Bezier curve network for real-time end-to-end text recognition
[after reading the series of must know] one of how to realize app automation without programming (preparation)
text 文本数据增强方法 data argumentation
Preliminary introduction to C miscellaneous lecture document