Enumeration algorithm 4-print the number of daffodils

"Daffodil number" refers to such a three-digit number, the sum of the cubes of the digits is equal to the number itself. For example, 153=1^3+5^3+3^3

Use enumeration to traverse all integers from 100 to 999.

How to separate the hundreds place, tens place, everybody
i=n/100 //hundred place
j=n/10%10 //ten place
k=n%10 // everybody

code:
 

#include<stdio.h>
void main()
{
	int i, j, k, n;
	printf("水仙花数是:");
	for (n = 100; n < 1000; n++)
	{
		i = n / 100;	/*百位上的数字*/
		j = n / 10 % 10;	/*十位上的数字*/
		k = n % 10;		/*个位上的数字*/
		if (i * 100 + j * 10 + k == i*i*i + j*j*j + k*k*k)
			printf("%5d", n);
	}
	printf("\n");
	getchar();
}

result:

 

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/104151968