自幂数(自恋数)

如果在一个固定的进制中,一个n位自然数等于自身各个数位上数字的n次幂之和,则称此数为自幂数。

例如:在十进制中,153是一个三位数,各个数位的3次幂之和为1^3+5^3+3^3=153,所以153是十进制中的自幂数。

在n进制中,所有小于n的正整数都为自幂数,比如2进制中1是自幂数,3进制中1和2都是自幂数,4进制中1,2和3都是自幂数......

这里我用两种方法用c输出10进制1~10位数中的自幂数

第一种:循环嵌套,让每个循环从0循环到9,最外层的循环从1开始(数字首位不能为零),来输出一个区间内所有的数,如两层循环可以输出10~99、三层循环可以输出100~999,然后在最内层循环当中判断这个数是否为自幂数,如果是则进行打印。

优点:运算速度快。

缺点:代码不好写,看的眼花头疼。

第二种:数字切割,直接定义一个变量x,如验证三位自幂数,就让x从100开始每次自加1至999,然后通过除法求余将x的个位、十位、百位上的数字分别切割下来存储到变量a、b、c三个变量当中,然后判断x=a^3+b^3+c^3是否成立,成立则进行打印。

优点:代码好写。

缺点:运算速度慢。

本人处理器为Intel(R)Core(TM) i7-9750H,在CPU负载15%的情况下,两种方法的运算时间如下:

循环嵌套法约  00:01:50

数字切割法约    00:09:19

二者运算速度差异如此之大的原因是数字切割法每次判断一个数是否为自幂数之前要先将数字进行切割,如判断三位自幂数时就需要进行999*3=2997次切割,而判断十位自幂数时就需要进行9999999999*10=99999999990次切割,这会浪费大量的时间,而循环嵌套法中的每一个数字中的每一位本身就是独立循环的,不需要进行切割,直接进行判断即可。

数字切割法的切割方法:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a = 54321, b,c,d,e,f;
	b = a % 10;//个位
	c = a / 10 % 10;//十位
	d = a / 100 % 10;//百位
	e = a / 1000 % 10;//千位
	f = a / 10000;//万位
	printf("%d %d %d %d %d\n",b,c,d,e,f);
	return 0;
}

循环嵌套法代码如下:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	printf("独身数:\n");
	int a1=0;
	while (a1 < 10)
	{
		if (a1 == a1)
			printf("%d^1=%d\n", a1,a1);
		a1++;
	}
	printf("比翼双飞数:\n");
	int a2 = 1, b2, c2=0;
	while (a2 < 10)
	{
		b2 = 0;
		while (b2 < 10)
		{
			if (10 * a2 + b2 == a2 * a2 + b2 * b2)
			{
				printf("%d^2+%d^2=%d\n", a2, b2, 10 * a2 + b2);
				c2++;
			}
			b2++;
		}
		a2++;
	}
	if (c2 == 0)
		printf("无\n");
	printf("水仙花数:\n");
	int a3 = 1, b3, c3;
	while (a3 < 10)
	{
		b3 = 0;
		while (b3 < 10)
		{
			c3 = 0;
			while (c3 < 10)
			{
				if (100 * a3 + 10 * b3 + c3 == a3 * a3 * a3 + b3 * b3 * b3 + c3 * c3 * c3)
					printf("%d^3+%d^3+%d^3=%d\n", a3, b3, c3, 100 * a3 + 10 * b3 + c3);
				//printf("%d%d%d ",a3,b3,z);
				c3++;
			}
			b3++;
		}
		a3++;
	}
	printf("四叶玫瑰数:\n");
	int a4 = 1, b4, c4, d4;
	while (a4 < 10)
	{
		b4 = 0;
		while (b4 < 10)
		{
			c4 = 0;
			while (c4 < 10)
			{
				d4 = 0;
				while (d4 < 10)
				{
					if (1000 * a4 + 100 * b4 + 10 * c4 + d4 ==
						a4 * a4 * a4 * a4 + b4 * b4 * b4 * b4 +
						c4 * c4 * c4 * c4 + d4 * d4 * d4 * d4)
						printf("%d^4+%d^4+%d^4+%d^4=%d\n",
							a4, b4, c4, d4, a4 * 1000 + b4 * 100 + c4 * 10 + d4);
					d4++;
				}
				c4++;
			}
			b4++;
		}
		a4++;
	}
	printf("五角星数:\n");
	int a5 = 1, b5, c5, d5, e5;
	while (a5 < 10)
	{
		b5 = 0;
		while (b5 < 10)
		{
			c5 = 0;
			while (c5 < 10)
			{
				d5 = 0;
				while (d5 < 10)
				{
					e5 = 0;
					while (e5 < 10)
					{
						if (10000 * a5 + 1000 * b5 + 100 * c5 + 10 * d5 + e5 ==
							a5 * a5 * a5 * a5 * a5 + b5 * b5 * b5 * b5 * b5 +
							c5 * c5 * c5 * c5 * c5 + d5 * d5 * d5 * d5 * d5 +
							e5 * e5 * e5 * e5 * e5)
							printf("%d^5+%d^5+%d^5+%d^5+%d^5=%d\n",
								a5, b5, c5, d5, e5, a5 * 10000 + b5 * 1000 + c5 * 100 + d5 * 10 + e5);
						e5++;
					}
					d5++;
				}
				c5++;
			}
			b5++;
		}
		a5++;
	}
	printf("六合数:\n");
	int a6 = 1, b6, c6, d6, e6, f6;
	while (a6 < 10)
	{
		b6 = 0;
		while (b6 < 10)
		{
			c6 = 0;
			while (c6 < 10)
			{
				d6 = 0;
				while (d6 < 10)
				{
					e6 = 0;
					while (e6 < 10)
					{
						f6 = 0;
						while (f6 < 10)
						{
							if (100000 * a6 + 10000 * b6 + 1000 * c6 + 100 * d6 + 10 * e6 + f6 ==
								a6 * a6 * a6 * a6 * a6 * a6 + b6 * b6 * b6 * b6 * b6 * b6 +
								c6 * c6 * c6 * c6 * c6 * c6 + d6 * d6 * d6 * d6 * d6 * d6 +
								e6 * e6 * e6 * e6 * e6 * e6 + f6 * f6 * f6 * f6 * f6 * f6)
								printf("%d^6+%d^6+%d^6+%d^6+%d^6+%d^6=%d\n",
									a6, b6, c6, d6, e6, f6, 100000 * a6 + 10000 * b6 +
									1000 * c6 + 100 * d6 + 10 * e6 + f6);
							f6++;
						}
						e6++;
					}
					d6++;
				}
				c6++;
			}
			b6++;
		}
		a6++;
	}
	printf("北斗七星数:\n");
	int a7 = 1, b7, c7, d7, e7, f7, g7;
	while (a7 < 10)
	{
		b7 = 0;
		while (b7 < 10)
		{
			c7 = 0;
			while (c7 < 10)
			{
				d7 = 0;
				while (d7 < 10)
				{
					e7 = 0;
					while (e7 < 10)
					{
						f7 = 0;
						while (f7 < 10)
						{
							g7 = 0;
							while (g7 < 10)
							{
								if (1000000 * a7 + 100000 * b7 + 10000 * c7 + 1000 * d7 + 100 * e7 + 10 * f7 + g7 ==
									a7 * a7 * a7 * a7 * a7 * a7 * a7 + b7 * b7 * b7 * b7 * b7 * b7 * b7 +
									c7 * c7 * c7 * c7 * c7 * c7 * c7 + d7 * d7 * d7 * d7 * d7 * d7 * d7 +
									e7 * e7 * e7 * e7 * e7 * e7 * e7 + f7 * f7 * f7 * f7 * f7 * f7 * f7 +
									g7 * g7 * g7 * g7 * g7 * g7 * g7)
									printf("%d^7+%d^7+%d^7+%d^7+%d^7+%d^7+%d^7=%d\n",
										a7, b7, c7, d7, e7, f7, g7, 1000000 * a7 + 100000 * b7 + 10000 * c7 +
										1000 * d7 + 100 * e7 + 10 * f7 + g7);
								g7++;
							}
							f7++;
						}
						e7++;
					}
					d7++;
				}
				c7++;
			}
			b7++;
		}
		a7++;
	}
	printf("八仙数:\n");
	int a8 = 1, b8, c8, d8, e8, f8, g8, h8;
	while (a8 < 10)
	{
		b8 = 0;
		while (b8 < 10)
		{
			c8 = 0;
			while (c8 < 10)
			{
				d8 = 0;
				while (d8 < 10)
				{
					e8 = 0;
					while (e8 < 10)
					{
						f8 = 0;
						while (f8 < 10)
						{
							g8 = 0;
							while (g8 < 10)
							{
								h8 = 0;
								while (h8 < 10)
								{
									if (10000000 * a8 + 1000000 * b8 + 100000 * c8 + 10000 * d8 + 1000 * e8 + 100 * f8 + 10 * g8 + h8 ==
										a8 * a8 * a8 * a8 * a8 * a8 * a8 * a8 + b8 * b8 * b8 * b8 * b8 * b8 * b8 * b8 +
										c8 * c8 * c8 * c8 * c8 * c8 * c8 * c8 + d8 * d8 * d8 * d8 * d8 * d8 * d8 * d8 +
										e8 * e8 * e8 * e8 * e8 * e8 * e8 * e8 + f8 * f8 * f8 * f8 * f8 * f8 * f8 * f8 +
										g8 * g8 * g8 * g8 * g8 * g8 * g8 * g8 + h8 * h8 * h8 * h8 * h8 * h8 * h8 * h8)
										printf("%d^8+%d^8+%d^8+%d^8+%d^8+%d^8+%d^8+%d^8=%d\n",
											a8, b8, c8, d8, e8, f8, g8, h8, 10000000 * a8 + 1000000 * b8 +
											100000 * c8 + 10000 * d8 + 1000 * e8 + 100 * f8 + 10 * g8 + h8);
									h8++;
								}
								g8++;
							}
							f8++;
						}
						e8++;
					}
					d8++;
				}
				c8++;
			}
			b8++;
		}
		a8++;
	}
	printf("九九重阳数:\n");
	int a9 = 1, b9, c9, d9, e9, f9, g9, h9, i9;
	while (a9 < 10)
	{
		b9 = 0;
		while (b9 < 10)
		{
			c9 = 0;
			while (c9 < 10)
			{
				d9 = 0;
				while (d9 < 10)
				{
					e9 = 0;
					while (e9 < 10)
					{
						f9 = 0;
						while (f9 < 10)
						{
							g9 = 0;
							while (g9 < 10)
							{
								h9 = 0;
								while (h9 < 10)
								{
									i9 = 0;
									while (i9 < 10)
									{
										if (100000000 * a9 + 10000000 * b9 + 1000000 * c9 + 100000 * d9 + 10000 * e9 + 1000 * f9 + 100 * g9 + 10 * h9 + i9 ==
											a9 * a9 * a9 * a9 * a9 * a9 * a9 * a9 * a9 + b9 * b9 * b9 * b9 * b9 * b9 * b9 * b9 * b9 +
											c9 * c9 * c9 * c9 * c9 * c9 * c9 * c9 * c9 + d9 * d9 * d9 * d9 * d9 * d9 * d9 * d9 * d9 +
											e9 * e9 * e9 * e9 * e9 * e9 * e9 * e9 * e9 + f9 * f9 * f9 * f9 * f9 * f9 * f9 * f9 * f9 +
											g9 * g9 * g9 * g9 * g9 * g9 * g9 * g9 * g9 + h9 * h9 * h9 * h9 * h9 * h9 * h9 * h9 * h9 +
											i9 * i9 * i9 * i9 * i9 * i9 * i9 * i9 * i9)
											printf("%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9=%d\n",
												a9, b9, c9, d9, e9, f9, g9, h9, i9, 100000000 * a9 + 10000000 * b9 + 1000000 * c9 +
												100000 * d9 + 10000 * e9 + 1000 * f9 + 100 * g9 + 10 * h9 + i9);
										i9++;
									}
									h9++;
								}
								g9++;
							}
							f9++;
						}
						e9++;
					}
					d9++;
				}
				c9++;
			}
			b9++;
		}
		a9++;
	}
	printf("十全十美数:\n");
	long long a10 = 1, b10, c10, d10, e10, f10, g10, h10, i10, j10;
	while (a10 < 10)
	{
		b10 = 0;
		while (b10 < 10)
		{
			c10 = 0;
			while (c10 < 10)
			{
				d10 = 0;
				while (d10 < 10)
				{
					e10 = 0;
					while (e10 < 10)
					{
						f10 = 0;
						while (f10 < 10)
						{
							g10 = 0;
							while (g10 < 10)
							{
								h10 = 0;
								while (h10 < 10)
								{
									i10 = 0;
									while (i10 < 10)
									{
										j10 = 0;
										while (j10 < 10)
										{
											if (1000000000 * a10 + 100000000 * b10 + 10000000 * c10 + 1000000 * d10 + 100000 * e10 + 10000 * f10 + 1000 * g10 + 100 * h10 + 10 * i10 + j10 ==
												a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 + b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 +
												c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 + d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 +
												e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 + f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 +
												g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 + h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 +
												i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 + j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10)
												printf("%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10=%lld\n",
													a10, b10, c10, d10, e10, f10, g10, h10, i10, j10, 1000000000 * a10 + 100000000 * b10 +
													10000000 * c10 + 1000000 * d10 + 100000 * e10 + 10000 * f10 + 1000 * g10 + 100 * h10 + 10 * i10 + j10);
											j10++;
										}
										i10++;
									}
									h10++;
								}
								g10++;
							}
							f10++;
						}
						e10++;
					}
					d10++;
				}
				c10++;
			}
			b10++;
		}
		a10++;
	}
	return 0;
}

数字切割法代码如下: 

#include<stdio.h>
#include<stdlib.h>
int main()
{
	printf("水仙花数:\n");
	int x = 100, a3, b3, c3;
	while (x < 1000)
	{
		a3 = x % 10;
		b3 = x / 10 % 10;
		c3 = x / 100;
		if (x == a3 * a3 * a3 + b3 * b3 * b3 + c3 * c3 * c3)
			printf("%d^3+%d^3+%d^3=%d\n", c3, b3, a3, x);
		x++;
	}
	printf("四叶玫瑰数:\n");
	x = 1000;
	int a4, b4, c4, d4;
	while (x < 10000)
	{
		a4 = x % 10;
		b4 = x / 10 % 10;
		c4 = x / 100 % 10;
		d4 = x / 1000;
		if (x == a4 * a4 * a4 * a4 + b4 * b4 * b4 * b4 + c4 * c4 * c4 * c4 + d4 * d4 * d4 * d4)
			printf("%d^4+%d^4+%d^4+%d^4=%d\n", d4,c4,b4,a4, x);
		x++;
	}
	printf("五角星数:\n");
	x = 10000;
	int a5, b5, c5, d5, e5;
	while (x < 100000)
	{
		a5 = x % 10;
		b5 = x / 10 % 10;
		c5 = x / 100 % 10;
		d5 = x / 1000 % 10;
		e5 = x / 10000;
		if (x == a5 * a5 * a5 * a5 * a5 + b5 * b5 * b5 * b5 * b5 + c5 * c5 * c5 * c5 * c5 + d5 * d5 * d5 * d5 * d5 + e5 * e5 * e5 * e5 * e5)
			printf("%d^5+%d^5+%d^5+%d^5+%d^5=%d\n", e5, d5, c5, b5, a5, x);
		x++;
	}
	printf("六合数:\n");
	x = 100000;
	int a6, b6, c6, d6, e6,f6;
	while (x < 1000000)
	{
		a6 = x % 10;
		b6 = x / 10 % 10;
		c6 = x / 100 % 10;
		d6 = x / 1000 % 10;
		e6 = x / 10000 % 10;
		f6 = x / 100000;

		if (x == a6 * a6 * a6 * a6 * a6 * a6 + b6 * b6 * b6 * b6 * b6 * b6 +
			c6 * c6 * c6 * c6 * c6 * c6 + d6 * d6 * d6 * d6 * d6 * d6 +
			e6 * e6 * e6 * e6 * e6 * e6 + f6 * f6 * f6 * f6 * f6 * f6)
			printf("%d^6+%d^6+%d^6+%d^6+%d^6+%d^6=%d\n",
				f6, e6, d6, c6, b6, a6, x);
		x++;
	}
	printf("北斗七星数:\n");
	x = 1000000;
	int a7, b7, c7, d7, e7, f7, g7;
	while (x < 10000000)
	{
		a7 = x % 10;
		b7 = x / 10 % 10;
		c7 = x / 100 % 10;
		d7 = x / 1000 % 10;
		e7 = x / 10000 % 10;
		f7 = x / 100000 % 10;
		g7 = x / 1000000;

		if (x == a7 * a7 * a7 * a7 * a7 * a7 * a7 + b7 * b7 * b7 * b7 * b7 * b7 * b7 +
			c7 * c7 * c7 * c7 * c7 * c7 * c7 + d7 * d7 * d7 * d7 * d7 * d7 * d7 +
			e7 * e7 * e7 * e7 * e7 * e7 * e7 + f7 * f7 * f7 * f7 * f7 * f7 * f7 +
			g7 * g7 * g7 * g7 * g7 * g7 * g7)
			printf("%d^7+%d^7+%d^7+%d^7+%d^7+%d^7+%d^7=%d\n",
				g7, f7, e7, d7, c7, b7, a7, x);
		x++;
	}
	printf("八仙数:\n");
	x = 10000000;
	int a8, b8, c8, d8, e8, f8, g8, h8;
	while (x < 100000000)
	{
		a8 = x % 10;
		b8 = x / 10 % 10;
		c8 = x / 100 % 10;
		d8 = x / 1000 % 10;
		e8 = x / 10000 % 10;
		f8 = x / 100000 % 10;
		g8 = x / 1000000 % 10;
		h8 = x / 10000000;

		if (x == a8 * a8 * a8 * a8 * a8 * a8 * a8 * a8 + b8 * b8 * b8 * b8 * b8 * b8 * b8 * b8 +
			c8 * c8 * c8 * c8 * c8 * c8 * c8 * c8 + d8 * d8 * d8 * d8 * d8 * d8 * d8 * d8 +
			e8 * e8 * e8 * e8 * e8 * e8 * e8 * e8 + f8 * f8 * f8 * f8 * f8 * f8 * f8 * f8 +
			g8 * g8 * g8 * g8 * g8 * g8 * g8 * g8 + h8 * h8 * h8 * h8 * h8 * h8 * h8 * h8)
			printf("%d^8+%d^8+%d^8+%d^8+%d^8+%d^8+%d^8+%d^8=%d\n",
				h8, g8, f8, e8, d8, c8, b8, a8, x);
		x++;
	}
	printf("九九重阳数:\n");
	x = 100000000;
	int a9, b9, c9, d9, e9, f9, g9, h9, i9;
	while (x < 1000000000)
	{
		a9 = x % 10;
		b9 = x / 10 % 10;
		c9 = x / 100 % 10;
		d9 = x / 1000 % 10;
		e9 = x / 10000 % 10;
		f9 = x / 100000 % 10;
		g9 = x / 1000000 % 10;
		h9 = x / 10000000 % 10;
		i9 = x / 100000000;

		if (x == a9 * a9 * a9 * a9 * a9 * a9 * a9 * a9 * a9 + b9 * b9 * b9 * b9 * b9 * b9 * b9 * b9 * b9 +
			c9 * c9 * c9 * c9 * c9 * c9 * c9 * c9 * c9 + d9 * d9 * d9 * d9 * d9 * d9 * d9 * d9 * d9 +
			e9 * e9 * e9 * e9 * e9 * e9 * e9 * e9 * e9 + f9 * f9 * f9 * f9 * f9 * f9 * f9 * f9 * f9 +
			g9 * g9 * g9 * g9 * g9 * g9 * g9 * g9 * g9 + h9 * h9 * h9 * h9 * h9 * h9 * h9 * h9 * h9 +
			i9 * i9 * i9 * i9 * i9 * i9 * i9 * i9 * i9)
			printf("%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9+%d^9=%d\n",
				i9, h9, g9, f9, e9, d9, c9, b9, a9, x);
		x++;
	}
	printf("十全十美数:\n");
	long long y;
	y = 1000000000;
	long long a10, b10, c10, d10, e10, f10, g10, h10, i10, j10;
	while (y < 10000000000)
	{
		a10 = y % 10;
		b10 = y / 10 % 10;
		c10 = y / 100 % 10;
		d10 = y / 1000 % 10;
		e10 = y / 10000 % 10;
		f10 = y / 100000 % 10;
		g10 = y / 1000000 % 10;
		h10 = y / 10000000 % 10;
		i10 = y / 100000000 % 10;
		j10 = y / 1000000000;

		if (y == a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 * a10 + b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 * b10 +
			c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 * c10 + d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 * d10 +
			e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 * e10 + f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 * f10 +
			g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 * g10 + h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 * h10 +
			i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 * i10 + j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10 * j10)
			printf("%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10+%lld^10=%lld\n",
				j10, i10, h10, g10, f10, e10, d10, c10, b10, a10, y);
		y++;
	}
	return 0;
}

二者运行效果皆如下

f36dbc57511342989e4b61932327c866.jpeg

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值