书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

经典进制转换——USACO Palindromic Squares

Description

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

Input

A single line with B, the base (specified in base 10).

Output

Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself.

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



这里简单说一下base的意思和用法,
base 10,就是十进制的意思

#include "iostream"
#include "string"
#include "cstring"
using namespace std;
int bit[20]={0};
char chbit[20]={'0'};
char forbit[20]={'0'};
int pos, TenBase, pos1;

void ChTen(int base, int num) //将十进制的数 转化成相应的进制 数组形式的
{
	int i;
	for(i=0; num>0; i++)
	{
		bit[i] = num%base;
		num /= base;		
	}
	pos = i-1; //统计字符串长度
}

bool Judge() //判断字符数组是否为回文
{
	pos1 = pos;
	for(int i=0; i<=pos; i++)
	{
		if(bit[i]<10)
			chbit[i] = bit[i]+48;
		else
			chbit[i] = bit[i] - 10 + 'A';
	}
	for(int i=0, j=pos1; i<=j; i++, j--)
		if(chbit[i]!=chbit[j])
			return false;
	return true;
}
int main()
{
	int base, TenNum;
	cin>>base;
	for(int m=1; m<=300; m++)
	{
		TenNum = m*m;
		ChTen(base, TenNum);
		if(Judge())
		{
			ChTen(base, m);
			for(int i=0; i<=pos; i++)
			{
				if(bit[i]<10)
					forbit[i] = bit[i]+48;
				else
					forbit[i] = bit[i] - 10 + 'A';
			}
			for(int i=pos; i>=0; i--)
				cout<<forbit[i];
			cout<<" ";
			for(int i=pos1; i>=0; i--)
				cout<<chbit[i];
			cout<<endl;
		}
	}
}

posted on 2011-10-16 11:00  More study needed.  阅读(231)  评论(0编辑  收藏  举报

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!