您的位置:首页 > 其它

USACO Section 1.2: Palindromic Squares

2014-03-16 13:34 513 查看
/*
ID: leetcod3
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <cmath>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <stack>

using namespace std;

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

string tobase(int x, int base) {
string ans = "";
while (x) {
int tmp = x % base;
if (tmp >= 10) ans.insert(0, 1, 'A'+tmp-10);
else ans.insert(0, 1, '0'+tmp);
x /= base;
}
return ans;
}

bool palin(string s) {
for (int i = 0; i < s.size() / 2; i++) {
if (s[i] != s[s.size()-1-i]) return false;
}
return true;
}

int main() {
int base;
fin >> base;
for (int i = 1; i <= 300; i++) {
if (palin(tobase(i*i, base))) fout << tobase(i, base) << " " << tobase(i*i, base) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: