1.2的第4第5题都考察了10进制转X进制的问题,用以前的模板套一下就好了
/*
ID: zhangw31
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;
int base;
string tentox (int num, int syst)
{
string returnstring;
string digt = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
stack<char>number;
while (num != 0)
{
number.push(digt[num % syst]);
num /= syst;
}
while (!number.empty())
{
returnstring += number.top();
number.pop();
}
return returnstring;
}
int xtoten (string num, int syst)
{
int xtotenn = 0;
int length = num.length();
for (int i = 0; i < length; ++i)
{
if (num[i] >= '0' && num[i] <= '9') xtotenn = xtotenn * syst + (num[i] - '0');
if (num[i] >= 'A' && num[i] <= 'Z') xtotenn = xtotenn * syst + (num[i] - 'A' + 10);
}
return xtotenn;
}
bool testPalindromic(string a) {
int len = a.length();
for (int i = 0; i < len / 2; ++i) {
if (a[i] != a[len-1-i]) return false;
}
return true;
}
int main() {
ifstream fin("palsquare.in");
ofstream fout("palsquare.out");
fin >> base;
for (int i = 1; i <= 300; ++i) {
string teststring = tentox(i*i, base);
if (testPalindromic(teststring)) {
fout << tentox(i, base) << " " << teststring << endl;
}
}
fin.close();
fout.close();
}