当前位置:网站首页>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 .
边栏推荐
- MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
- Mysql36 database backup and recovery
- A necessary soft skill for Software Test Engineers: structured thinking
- South China Technology stack cnn+bilstm+attention
- Export virtual machines from esxi 6.7 using OVF tool
- Installation of pagoda and deployment of flask project
- 13 医疗挂号系统_【 微信登录】
- MySQL27-索引优化与查询优化
- MySQL28-数据库的设计规范
- 实现以form-data参数发送post请求
猜你喜欢
[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in
The underlying logical architecture of MySQL
Mysql32 lock
数据库中间件_Mycat总结
【C语言】深度剖析数据存储的底层原理
[after reading the series of must know] one of how to realize app automation without programming (preparation)
Download and installation of QT Creator
Use xtrabackup for MySQL database physical backup
MySQL storage engine
Security design verification of API interface: ticket, signature, timestamp
随机推荐
Installation of pagoda and deployment of flask project
保姆级手把手教你用C语言写三子棋
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
13 医疗挂号系统_【 微信登录】
Sed text processing
Routes and resources of AI
How to make shell script executable
MySQL31-MySQL事务日志
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
软件测试工程师发展规划路线
What is the current situation of the game industry in the Internet world?
Mysql36 database backup and recovery
Pytoch LSTM implementation process (visual version)
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
如何让shell脚本变成可执行文件
Security design verification of API interface: ticket, signature, timestamp
实现微信公众号H5消息推送的超级详细步骤
16 medical registration system_ [order by appointment]
MySQL底层的逻辑架构
ZABBIX introduction and installation