-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 36
More file actions
79 lines (71 loc) · 1.74 KB
/
Copy pathproblem 36
File metadata and controls
79 lines (71 loc) · 1.74 KB
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
/*
The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
*/
#include <iostream>
#include<string>
#include<vector>
#include <cmath>
using namespace std;
void convert_to_binary(int n , vector<int> &binary){
if(n / 2 != 0){
convert_to_binary(n / 2 , binary);
}
binary.push_back(n % 2);
}
bool pal(int num){
string digit = to_string(num);
if(digit.size() == 1){
return true;
}
if(digit.size() == 2 && digit[0] == digit[1]){
return true;
}
if(digit.size() == 3 && digit[0] == digit[2]){
return true;
}
if(digit.size() == 4 && digit[0] == digit[3] && digit[1] == digit[2]){
return true;
}
if(digit.size() == 5 && digit[0] == digit[4] && digit[1] == digit[3]){
return true;
}
if(digit.size() == 6 && digit[0] == digit[5] && digit[1] == digit[4] && digit[3] == digit[2]){
return true;
}
if(digit.size() == 7 && digit[0] == digit[6] && digit[1] == digit[5] && digit[2] == digit[4]){
return true;
}
return false;
}
int pal_binary(vector<int> &binary){
bool flag = true;
int n = binary.size() - 1;
for(int i = 0; i < floor(binary.size() / 2); i++){
if(binary[i] != binary[n - i]){
flag = false;
}
}
binary.clear();
return flag;
}
int main(){
int result = 0;
vector<int> binary;
for(int i = 1; i < 1000000; i++){
if(pal(i)){
convert_to_binary(i , binary);
if(pal_binary(binary)){
result += i;
}
}
}
cout << result <<endl;
/*
convert_to_binary(313, binary);
for(int x : binary){
cout << x <<endl;
}*/
return 0;
}