-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q36.c
58 lines (55 loc) · 1.11 KB
/
Q36.c
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
#include <stdio.h>
#define true 1
#define false 0
#define LENGTH_BASE10 7
#define LENGTH_BASE2 20
int is_palindrome(int number) {
int digits[LENGTH_BASE10] = {0};
int i;
for(i=LENGTH_BASE10-1; i>=0; i--) {
int digit = number % 10;
number = (number - digit)/10;
digits[i] = digit;
if (number == 0) break;
}
int j = LENGTH_BASE10-1;
int flag = true;
int bound = (LENGTH_BASE10+i)/2;
for (i=i;i<=bound; i++, j--) {
if (i==j) break;
if (digits[i]!=digits[j]) {
flag = false;
break;
}
}
return flag;
}
int is_palindrome_binary(int number) {
int digits[LENGTH_BASE2] = {0};
int i = LENGTH_BASE2-1;
while (number != 0) {
digits[i] = number % 2;
number /= 2;
i--;
}
int j = LENGTH_BASE2-1;
int flag = true;
int bound = (LENGTH_BASE2+i)/2;
i++;
for (;i<=bound; i++, j--) {
if (i==j) break;
if (digits[i]!=digits[j]) {
flag = false;
break;
}
}
return flag;
}
int main() {
int sum=0;
for (int i=0; i<1000000;i++) {
if (is_palindrome(i) && is_palindrome_binary(i)) sum +=i;
}
printf("%d\n", sum);
return 0;
}