1.2.4 Palindromic Squares

  简单题……

#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;

ifstream fin("palsquare.in");
ofstream fout("palsquare.out");

void Trans(int num, int base, char str[] ) 
{
	int i=0, temp;
	while( num!=0 )
	{
		temp=num%base;
		num/=base;

		if( temp>=10 )
			str[i++]=temp+'A'-10;
		else 
			str[i++]=temp+'0';
	}
	str[i]='\0';
}
bool Judge(char str[])
{
	int i, j;
	for(j=0, i=strlen(str)-1; j<i; j++, i--)//不该写为j!=i,而应该是j<i 
	{
		if( str[i]!=str[j])
			return 0;
	}

	return 1;	
}
void Print( char str[])
{
	int i=0;
	for(i=strlen(str)-1; i>=0;  i--)
		fout<<str[i];
}
int main()
{
	int i, base;
	char str1[50], str2[50];
	fin>>base;
	for(i=1; i<=300; i++)
	{
		Trans(i*i, base, str2);
		if( Judge( str2 ))
		{
			Trans(i, base, str1);
			
			Print(str1);
			fout<<" ";
			Print( str2);
			fout<<endl;
		}	
	}	
}


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