当前位置:网站首页>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 .
边栏推荐
- The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
- 基于Pytorch的LSTM实战160万条评论情感分类
- 在jupyter NoteBook使用Pytorch进行MNIST实现
- Transactions have four characteristics?
- Solve the problem of remote connection to MySQL under Linux in Windows
- Solution to the problem of cross domain inaccessibility of Chrome browser
- 解决在window中远程连接Linux下的MySQL
- 数据库中间件_Mycat总结
- MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
- Bytetrack: multi object tracking by associating every detection box paper reading notes ()
猜你喜欢
实现微信公众号H5消息推送的超级详细步骤
MySQL32-锁
Use JUnit unit test & transaction usage
MySQL 29 other database tuning strategies
13 medical registration system_ [wechat login]
MySQL36-数据库备份与恢复
Case identification based on pytoch pulmonary infection (using RESNET network structure)
jar运行报错no main manifest attribute
MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
Solve the problem of remote connection to MySQL under Linux in Windows
随机推荐
Mysql36 database backup and recovery
MNIST implementation using pytoch in jupyter notebook
如何让shell脚本变成可执行文件
MySQL31-MySQL事务日志
Anaconda3 installation CV2
Set shell script execution error to exit automatically
UEditor国际化配置,支持中英文切换
MySQL combat optimization expert 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
Software test engineer development planning route
Implement sending post request with form data parameter
【C语言】深度剖析数据存储的底层原理
MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
在jupyter NoteBook使用Pytorch进行MNIST实现
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
Const decorated member function problem
Solve the problem of remote connection to MySQL under Linux in Windows
MySQL ERROR 1040: Too many connections
C miscellaneous shallow copy and deep copy
text 文本数据增强方法 data argumentation
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant