当前位置:网站首页>【C语言】每日一题,求水仙花数,求变种水仙花数
【C语言】每日一题,求水仙花数,求变种水仙花数
2022-08-11 07:15:00 【零玖零】
1. 求出0~100000之间的所有“水仙花数”并输出。
“水仙花数”是指一个n位数,其各位数字的n次方之和确好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。
#include <stdio.h>
#include <math.h>
int main()//求水仙花数
{
int a = 0;
int mai = 0;
for (a = 0; a < 100000; a++)
{
int b = a;
int c = 0;
int sum = 0;
while (b>0)
{
c++;//利用c统计a的位数
b/=10;
}
b = a;
while (b > 0)
{
int ca = (int)pow(b % 10, c);//pow是用来求次方的库函数,其返回值是double类型
sum += ca;
b /= 10;
}
if (sum == a)
{
printf("%d ", a);
mai++;
}
}
printf("找到水仙花数个数为%d\n",mai);
}
此处我们用到了一个库函数pow,它的作用是求x的y次方。头文件math.h。当然也可以自己写一个函数去实现不必过于纠结。
其返回类型是一个double类型,在使用的过程中要注意, double pow(double x, double y)。
二、变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461),(14和61),(146和1),如果所有拆分后的乘积之和等于自身,则是一个Lily Number。
例如:
655 = 6 * 55 + 65 * 5
1461 = 1*461 + 14*61 + 146*1
求出 5位数中的所有 Lily Number。
#include <stdio.h>
int main()
{
int a=0;
for(a=10000;a<100000;a++)
{
int sum=0;
int b=a;
int cj=10;
while(b>=10)
{
int k=a%cj;
b/=10;
sum += k*b ;
cj*=10;
}
if(sum==a)
printf("%d ",a);
}
return 0;
}
思路
以12345为例,我们需要求出1*2345+12*345+123*45+1234*5值与12345进行比较,所以我们可以让12345/10得到1234,%10得到5.除以100的到123,模上100的到45.除以1000的到12,模上1000的到345.沿着这个思路我们很简单就可以得到一个代码。然后我们只要加一个循环让它遍历我所想要找的区间就得到了答案。
此题属于简单题,巧用除跟模便可以解决。
边栏推荐
- 租房小程序
- 2022-08-09 Group 4 Self-cultivation class study notes (every day)
- tf中矩阵乘法
- 【LaTex-错误和异常】\verb ended by end of line.原因是因为闭合边界符没有在\verb命令所属行中出现;\verb命令的正确和错误用法、verbatim环境的用法
- Tensorflow中使用tf.argmax返回张量沿指定维度最大值的索引
- DDR4内存条电路设计
- Evolution and New Choice of Streaming Structured Data Computing Language
- Pico neo3 Unity Packaging Settings
- JUC并发编程
- 分布式锁-Redission - 缓存一致性解决
猜你喜欢
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
2022 China Soft Drink Market Insights
1091 N-自守数 (15 分)
JRS303-数据校验
2.1-梯度下降
【LaTex-错误和异常】\verb ended by end of line.原因是因为闭合边界符没有在\verb命令所属行中出现;\verb命令的正确和错误用法、verbatim环境的用法
leetcode:69. x 的平方根
1.2-误差来源
JRS303-Data Verification
Test cases are hard?Just have a hand
随机推荐
Hibernate 的 Session 缓存相关操作
Active users of mobile banking grew rapidly in June, hitting a half-year high
关于Excel实现分组求和最全文档
XXL-JOB 分布式任务调度中心搭建
2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?
DDR4内存条电路设计
Serverless + domain name can also build a personal blog? Really, and soon
1046 划拳 (15 分)
1.1-回归
1101 How many times B is A (15 points)
3GPP LTE/NR信道模型
Evolution and New Choice of Streaming Structured Data Computing Language
囍楽cloud task source code
2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
1071 小赌怡情 (15 分)
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
Four states of Activity
求职简历这样写,轻松搞定面试官