当前位置:网站首页>每日一题——“水仙花数”
每日一题——“水仙花数”
2022-07-02 15:22:00 【保护小周ღ】
哈喽大家好,我是保护小周ღ,本期为大家带来的是求“水仙花数”,此水仙花,非彼水仙花,一起来看看把~
题目:
求出0~100000之间的所有“水仙花数”并输出。
描述:
“水仙花数”是指一个n位数,其各位数字的n次方之和刚好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。
注意范围是0~100000之间的所有符合描述的“水仙花数”。
思路解析:
根据描述,我们知道,各位数字的n次方之和刚好等于该数本身才能称之为“水仙花数”,先尝试判断一个数是否为水仙花数,怎么判断呢?
第一步:求出该数的位数n。
第二步:求出该数每一位的n次方之和。
第三步:判断该数字的n次方之和是否刚好等于该数本身
本次博主给大家带来两种解题方式,思路都是一样的,普通计数,递归计数。
普通计数:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//统计每一个数的位数
int Count(int size)
{
int n = 0;
while (size)
{
size = size / 10;
++n;
}
return n;
}
//判断是否为水仙花
int IfDaffodil(int i,int n)
{
int size = i;
int sum = 0;
//判断是否为符合“水仙花”
while (size!=0)
{
int ssum = 1;
//ssum统计每一位的n次方
for (int j = 0; j < n; j++)
{
ssum *= size % 10;
}
size = size / 10;
//sum统计各位的n次方之和
sum += ssum;
}
//各位数字的n次方之和确好等于该数本身
if (sum == i)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
//遍历区间
for (int i = 0; i <= 100000; ++i)
{
//当程序执行IfDaffodil时,会先执行完作为“参数”的Count,等Count执行完毕再执行自己
int size=IfDaffodil(i, Count(i));
if (size == 1)
{
printf("%d ",i);
}
else
{
continue;
}
}
return 0;
}
递归计数:
#include<stdio.h>
//统计每一个数的位数
int Count(int n)
{
if (n < 10)//当该数只剩个位时,递归结束
return 1;
else
return Count(n / 10) + 1;
/*
{
n=n/10;
return Count(n)+1;
}
*/
}
//计算每一位的n次方
int Pow(int x, int n)
{
if (n == 0)
return 1;
else
return pow(x, --n) * x;//x^n
}
int main()
{
//遍历区间
for (int i = 0; i < 100000; ++i)
{
int n = i;
int sum = 0;
//判断是否为符合“水仙花”
while (n)
{
//n % 10 拿到最后一位,然后根据本身的位数求次方
sum += (Pow((n % 10), Count(i)));//Pow计算每一位的n次方
n /= 10;
}
if (sum == i)
{
printf("%d ", sum);
}
else
{
continue;
}
}
return 0;
}
感兴趣的朋友可以用博主的方法,或者是自己的方法做做这道题。
分享一个牛客网上类似的题目,大家也可以尝试着做一做。
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ **,°*:.*( ̄▽ ̄)/$:*.°**
如有侵权请联系修改删除!
边栏推荐
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- 13、Darknet YOLO3
- easyswoole3.2重启不成功
- Blog theme "text" summer fresh Special Edition
- TCP拥塞控制详解 | 2. 背景
- 第十五章 字符串本地化和消息字典(一)
- Platform management background and business menu resource management: business permissions and menu resource management design
- uniapp H5页面调用微信支付
- ROS knowledge point - message_filters
- Migrate your accelerator based SAP commerce cloud storefront to Spartacus
猜你喜欢
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
阿里天池SQL学习笔记——DAY3
What if the default browser cannot be set?
Qstype implementation of self drawing interface project practice (II)
Si446 usage record (II): generate header files using wds3
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
深度之眼(二)——矩阵及其基本运算
【目标跟踪】|数据集汇总
RK1126平台项目总结
VirtualLab基础实验教程-7.偏振(2)
随机推荐
Microservice architecture practice: using Jenkins to realize automatic construction
海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
JDBC
What are the green field and brown field models in software development - green field development and brown field development
Un an à dix ans
TCP congestion control details | 2 background
The difference between class and getClass ()
【网络是怎样连接的】第五章 探索服务器
dstat使用[通俗易懂]
executescalar mysql_ ExecuteScalar()
Five reasons to choose SAP Spartacus as the implementation framework of SAP commerce cloud storefront
13、Darknet YOLO3
LeetCode:1380. Lucky number in matrix -- simple
traceroute命令讲解
ssb门限_SSB调制「建议收藏」
线性规划例题 投资的收益与风险
Configure lamp+supervisor
uva1169
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
VirtualLab基础实验教程-7.偏振(1)