-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalsquare.cpp
82 lines (65 loc) · 1.47 KB
/
palsquare.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
ID: th3c0r11
TASK: palsquare
LANG: C++14
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
int base;
bool palindrome(const std::string &str)
{
return std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin(), str.rbegin() + str.size() / 2);
}
static std::array<char, 20> list { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
struct BaseInt {
int value;
std::array<int, 10> repr;
bool operator<=(int n)
{
return value <= n;
}
BaseInt &operator++()
{
++value;
int i = 9;
while (++repr[i] == base) {
repr[i] = 0;
--i;
}
return *this;
}
operator std::string()
{
std::string ret;
int i = 0;
for ( ; repr[i] == 0; ++i);
for (; i < repr.size(); ++i) {
ret += list[repr[i]];
}
return ret;
}
std::string squareVal()
{
int square = value * value;
std::string ret;
while (square) {
ret += list[square % base];
square /= base;
}
return ret;
}
};
int main()
{
std::ifstream in{"palsquare.in"};
std::ofstream out{"palsquare.out"};
in >> base;
in.ignore(100, '\n');
for (BaseInt i{1, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}; i <= 300; ++i) {
if (palindrome(i.squareVal()))
out << std::string(i) << ' ' << i.squareVal() << '\n';
}
}