#161-[进制转换]回文平方数

Description

回文数是指从左向右念和从右向左念都一样的数。如12321就是一个典型的回文数。

给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数。用’A’,’B’……表示10,11等等

Input

共一行,一个单独的整数B(B用十进制表示)。

Output

每行两个B进制的符合要求的数字,第二个数是第一个数的平方,且第二个数是回文数。

Sample Input

10

Sample Output

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696

直接进制转换然后判断

#include <iostream>
#include <cstdio>
#include <cstring>

#define SIZE 210

using namespace std;

int a[SIZE], temp[SIZE], n, len, len2;

bool check(void) // 判断回文
{
	int i;
	
	for (i = 1; i <= len2; ++i)
	{
		if (temp[i] != temp[len2-i+1])
		{
			return false;
		}
	}
	
	return true;
}

void mul(void) // 高精度乘
{
	int i, j;
	
	len2 = len << 1;
	memset(temp, 0, sizeof (temp));
	for (i = 1; i <= len; ++i)
	{
		for (j = 1; j <= len; ++j)
		{
			temp[i+j-1] += a[i] * a[j];
		}
	}
	for (i = 1; i <= len2; ++i)
	{
		if (temp[i] >= n)
		{
			temp[i+1] += temp[i] / n;
			temp[i] %= n;
		}
	}
	while (!temp[len2])
	{
		--len2;
	}
	
	return;
}

int main(void)
{
	int i, j, temp;
	
	scanf("%d", &n);
	
	for (i = 1; i <= 300; ++i)
	{
		memset(a, 0, sizeof (a));
		temp = i;
		len = 0;
		while (temp) // 进制转换
		{
			a[++len] = temp % n;
			temp /= n;
		}
		mul();
		if (check())
		{
			for (j = len; j; --j)
			{
				if (a[j] > 9)
				{
					printf("%c", 'A' + a[j] - 10);
				}
				else
				{
					printf("%d", a[j]);
				}
			}
			printf(" ");
			for (j = len2; j; --j)
			{
				if (::temp[j] > 9)
				{
					printf("%c", 'A' + ::temp[j] - 10);
				}
				else
				{
					printf("%d", ::temp[j]);
				}
			}
			printf("\n");
		}
	}
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值