usaco 1.3.4: palindromic squares

题意

洛谷 P1206

分析

直接枚举就可以。进制转换不是很难,但是我遇到了一个小bug:在<=4.8.0的MinGW中,stoito_string等几个函数是没法直接用的。[1]这个问题有解决方案,但是我懒得去修了,总之我发现可以直接把char赋给string

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

ID: zhanghu15
TASK: palsquare
LANG: C++14
*/


#include <fstream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

map<int, string> digitMap;

void () {
// 可以把char赋给string
for (int i = 0; i < 10; i++)
digitMap[i] = char(i + '0'); // to_string does not work, a known issue
for (int i = 10; i <= 20; i++)
digitMap[i] = (char) (i - 10 + 'A');
}

string base10ToB(int B, int x) {
string baseB;
if (x == 0)
return "0";
while (x > 0) {
baseB = digitMap[x % B] + baseB;
x /= B;
}
return baseB;
}

bool checkIsPalindrome(string x) {
int n = x.length();
for (int i = 0; i + i < n; i++)
if (x[i] != x[n - i - 1])
return false;
return true;
}

int main() {
ofstream fout("palsquare.out");
ifstream fin("palsquare.in");
int B;
fin >> B;

init();

for (int i = 1; i <= 300; i++) {
string square = base10ToB(B, i * i);
if (checkIsPalindrome(square)) {
fout << base10ToB(B, i) << ' ' << square << endl;
}
}

return 0;
}

  1. Bug 52015 - std::to_string does not work under MinGW