习题4-6 水仙花数 (20 分)

习题4-6 水仙花数 (20 分)
水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:在这里插入图片描述

本题要求编写程序,计算所有N位水仙花数。

输入格式:
输入在一行中给出一个正整数N(3≤N≤7)。

输出格式:
按递增顺序输出所有N位水仙花数,每个数字占一行。

输入样例:
3
输出样例:
153
370
371
407

#include<stdio.h>
int main()
{
    
    
	int x = 1 , y = 1;
	int  n;
	scanf("%d",&n);
	for(int i = 1 ; i < n ;i ++)
	{
    
    
		x *= 10;
		y *= 10;
	}
	y *= 10;
	for(int i = x ;i < y ;i ++)
	{
    
    
		int t = i,sum = 0;
		while(t != 0)
		{
    
    
			int m = n,tt = 1, l = t%10;
			while(m--)
			{
    
    
				tt*=l;
				
			}
			sum += tt;
			
			t /= 10;
			//
		}
		//printf("sum = %d\n",sum);
		if(sum==i)
			printf("%d\n",i);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ChaoYue_miku/article/details/115221610
今日推荐