C language (find the number of daffodils between 0 and 100,000)

Baidu Encyclopedia: Narcissus number (Narcissistic number) is also known as super complete digital invariant (pluperfect digital invariant, PPDI), narcissistic number, self-power number, Armstrong number or Armstrong number (Armstrong number), narcissus A number refers to a 3-digit number, the sum of the 3 powers of the numbers in each of its digits is equal to itself. For example: 1^3 + 5^3+ 3^3 = 153.
The idea of ​​​​solving the problem, we can first find out that a number has n digits, and then divide it into hundreds. . Use the pow function to sum to the nth power, and finally compare
the source code as follows:
#include<stdio.h>
#include<math.h>//When using the pow function, use
int main()
{ int i = 0;

for ( i = 0; i <= 100000; i++)
{
	int n = 0;//代表有几位数,比如123,那么n就为3,
	int tem = i;//使用一个临时变量来存放i,用来进行判断等操作,如若不然,会改变i的值,导致死循环
	while (tem)//求n的值,
	{
		n++;
		tem = tem / 10;

	}
	tem = i;//重新赋值,
	int sum = 0;//用于存放个位十位百位n次方的累加和
	while (tem)
	{
		sum += pow(tem % 10, n);
		tem = tem / 10;

	}
	if (sum == i)//判断
	{
		printf("%d\n", i);

	}
	
}
return 0;

}

Guess you like

Origin blog.csdn.net/Kirihara_Yukiho/article/details/123159754