USACO 1.2.4 – Palindromic Squares


/*
 ID: mohmdad5
 LANG: C++
 TASK: palsquare
 */

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int isp(string s) {
	int i = -1, j = s.length();
	while (i < j && s[++i] == s[--j]);
	return i >= j;
}

string itoa(int val, int base) {
	string buf;
	do {
		buf += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[ val % base];
		val /= base;
	} while ( val );
	return string(buf.rbegin(), buf.rend());
}

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

	int B;
	fin >> B;
	for (int i = 1; i < 301; i++)
		if (isp(itoa(i * i,  B)))
			fout << itoa(i, B) << " " << itoa(i * i,  B) << endl;
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: