当前位置:网站首页>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 .
边栏推荐
- C miscellaneous shallow copy and deep copy
- MySQL ERROR 1040: Too many connections
- C miscellaneous two-way circular linked list
- MySQL27-索引优化与查询优化
- South China Technology stack cnn+bilstm+attention
- ZABBIX introduction and installation
- 数据库中间件_Mycat总结
- What is the current situation of the game industry in the Internet world?
- Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
- Use of dataset of pytorch
猜你喜欢
MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
如何搭建接口自动化测试框架?
MySQL27-索引优化与查询优化
17 medical registration system_ [wechat Payment]
jar运行报错no main manifest attribute
Super detailed steps for pushing wechat official account H5 messages
MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位
随机推荐
UEditor国际化配置,支持中英文切换
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
Const decorated member function problem
The programming ranking list came out in February. Is the result as you expected?
Use JUnit unit test & transaction usage
高并发系统的限流方案研究,其实限流实现也不复杂
Isn't there anyone who doesn't know how to write mine sweeping games in C language
13 医疗挂号系统_【 微信登录】
在jupyter NoteBook使用Pytorch进行MNIST实现
MySQL34-其他数据库日志
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
Good blog good material record link
颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
16 medical registration system_ [order by appointment]
C miscellaneous two-way circular linked list
Pytorch RNN actual combat case_ MNIST handwriting font recognition