USACO 1.2 Palindromic Squares

直接暴力判断即可


#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int a[100], b[100];
void resort(int n, int k)//n在k进制下的分解数字
{
	int tmp = n;
	int t = 0;
	n = n * n;
	while (n)
	{
		a[t ++] = n % k;
		n /= k;
	}
	for (int i = 0; i != t / 2; ++ i)	
		if (a[i] != a[t - i - 1])	return ;
	
	int s = 0;
	while (tmp)
	{
		b[s ++] = tmp % k;
		tmp /= k;
	}
	for (int i = s - 1; i >= 0; -- i)
	{
		if (b[i] > 9)
		{
			printf("%c", b[i] - 10 + 'A');
			continue;
		}
		printf("%d", b[i]);	
	}
	printf(" ");
	for (int i = t - 1; i >= 0; -- i)
	{
		if (a[i] > 9)
		{
			printf("%c", a[i] - 10 + 'A');
			continue;
		}
		printf("%d", a[i]);
	}
	printf("\n");
}

int main()
{
	int k;
	scanf("%d", &k);
	for (int i = 1; i <= 300; ++ i)
		resort(i, k);
	return 0;
}


你可能感兴趣的:(USACO 1.2 Palindromic Squares)