输出给定范围的水仙花数

首先给定一个数字
由程序求判断数字的位数
再求该数每一位数的次方和
最后判断是否相等

int main()
{
	int i = 0;
	int n = 0;
	scanf("%d",&n);
	for(i=0; i<=n; i++)
	{
		int count = 1;
		int tmp = i;
		int sum = 0;
//计算次方	
		while(tmp/10)
		{
			count++;
			tmp = tmp/10;
		}
		
		tmp = i;
//每一位的次方和,算完之后重新tmp归i
		while(tmp)
		{
			sum += pow(tmp%10, count);
			tmp = tmp/10;
		}

//判断是否相等
	
		if(sum == i)
			printf("%d ", i);
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43220266/article/details/82866056