-
Notifications
You must be signed in to change notification settings - Fork 1
/
RotationallySymmetricNumbers.cpp
103 lines (82 loc) · 1.86 KB
/
RotationallySymmetricNumbers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <unordered_map>
#include <set>
#include <string>
#include <vector>
using namespace std;
bool isRotationallySymmetricNumber(string src)
{
if (src.empty())
{
return true;
}
string tar;
unordered_map<char, char> m = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
int n = src.size(), n2 = n / 2;
for (int i = 0; i <= n2; ++i)
{
if (m[src[i]]!= src[n - 1 - i])
{
return false;
}
}
return true;
//for (auto c: src)
//{
//if (m.find(c) != m.end())
//{
//tar += m[c];
//}
//else
//{
//return false;
//}
//}
//reverse(tar.begin(), tar.end());
//return src == tar;
}
void generate(int n, unordered_map<char, char> &m, set<string> &s, string str)
{
if (n <= 0)
{
if (isRotationallySymmetricNumber(str))
{
s.insert(str);
}
return;
}
for (auto itr = m.begin(); itr != m.end(); ++itr)
{
str.push_back(itr->second);
generate(n - 1, m, s, str);
// don't forget to pop it out.
str.pop_back();
}
}
set<string> generateRotationallySymmetricNumbers(int n)
{
set<string> s;
if (n < 0)
{
return s;
}
unordered_map<char, char> m = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
string str;
generate(n, m, s, str);
return s;
}
int main()
{
//vector<string> nums = {"0", "1", "8"};
//vector<string> nums = {"2", "3", "."};
vector<string> nums = {"00", "11", "88", "69", "96"};
for (auto num : nums)
{
std::cout << isRotationallySymmetricNumber(num) << std::endl;
}
set<string> s = generateRotationallySymmetricNumbers(4);
for (auto str : s)
{
std::cout << str << std::endl;
}
}