2

水仙花数--(自幂数)

 1 year ago
source link: https://blog.51cto.com/u_15501985/5912532
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

题目名称:

打印水仙花数

题目内容:

求出0~100000之间的所有“水仙花数”并输出。

“水仙花数”是指一个n位数,其各位数字的n次方之和确好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。

水仙花数介绍:

水仙花数--(自幂数)_十进制

水仙花数--(自幂数)_十进制_02

输入0到100000(i),进行判断是否为水仙花数,然后,要判断i是几位数(n),

其次是计算(i)的十进制的每一位的n次方之和,最后就是判断是否等于本身

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
for (i = 0; i < 100000; i++)
{
//判断i是几位数(n)
int n = 1;//(0)最小从一位开始
int tmp = i;//要改变i的值,但是在i循环里面重新设变量
while (tmp > 9)
{
tmp /= 10;
n++;
}
//计算i的十进制的n次方,判断sum是否等于i
tmp = i;
int sum = 0;
while (tmp)
{
sum += (int)pow(tmp % 10, n);
tmp /= 10;
}
if (sum == i)
printf("%d ", i);
}
return 0;
}

运行结果:

水仙花数--(自幂数)_#include_03

趁热打铁-----没有撤退可言-------下期再会

 ​https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703​


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK