水仙花问题

160 阅读2分钟

水仙花.jpg

代码如下:

#include<stdio.h>
#include<math.h>
void main()
{
	int N;//位数
	int i;//数字
	int g, s, b, q, w, sw, bw;//个位 十位 百位 千位 万位 十万位 百万位
	printf("请输入一个正整数:(3<=N<=7)\n");
	scanf_s("%d", &N);//位数
	int max = pow(10, N);
	int min = pow(10, N - 1);
	//printf("%d\n", min);//测试
	//printf("%d\n", max);//测试
	for(i=min; i <max;i++)
	{
		g = i % 10;//个位
		s = i / 10 % 10;//十位
		if (N == 3)
		{
			b = i / 100;//百位
			if (g * g * g + s * s * s + b * b * b ==i)
			{
				printf("%d\n", i);
			}
		}
		if (N == 4)
		{
			b = i / 100%10;//百位
			q = i / 1000;//千位
			if (g * g * g * g + s * s * s * s + b * b * b * b  + q * q * q * q == i)
			{
				printf("%d\n", i);
			}
		}
		if (N == 5)
		{
			b = i / 100%10;//百位
			q = i / 1000%10;//千位
			w = i / 10000;//万位
			if (g * g * g * g * g  + s * s * s * s * s + b * b * b * b * b + q * q * q * q * q +w * w * w * w * w == i)
			{
				printf("%d\n", i);
			}
		}
		if (N == 6)
		{
			b = i / 100%10;//百位
			q = i / 1000%10;//千位
			w = i / 10000%10;//万位
			sw = i /100000;//十万位
			if (g * g * g * g * g * g + s * s * s * s * s * s + b * b * b * b * b * b + q * q * q * q  * q * q + w * w * w * w * w * w + sw * sw * sw * sw * sw * sw == i)
			{
				printf("%d\n", i);
			}
		}
		if (N == 7)
		{

			b = i / 100%10;//百位
			q = i / 1000%10;//千位
			w = i / 10000%10;//万位
			sw = i / 100000%10;//十万位
			bw = i / 1000000;//百万位
			if (g * g * g * g * g * g * g + s * s * s * s * s * s * s + b * b * b * b * b * b * b + q * q * q * q * q * q * q + w * w * w * w * w * w * w + sw * sw * sw * sw * sw * sw * sw + bw * bw * bw * bw * bw * bw * bw == i)
			{
				printf("%d\n", i);
			}
		}
	}
	return 0;
}

问题如下: 1.进到4-7位数时,忘记对上一位进行取余。
2.i=多少次方相乘时,在4-7位时忘记进行对应的次方。
3.用pow这一几次方时,要记得引用math.h。
4.if后面的等于要用==这一符号表示比较。
5.此题需先计算得出各个位数怎么表示出来。