summaryrefslogtreecommitdiff
path: root/bio/1/1.c
blob: d08611954741894dd937b1805609c71e3b13eb7f (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

char *reverse(char *s){
	char *rev = malloc(strlen(s));
	int j = 0;
	for (int i = strlen(s)-1; i >= 0; i--){
		rev[j] = s[i];
		j++;
	}
	rev[j] = 0;

	return rev;
}

bool ispal(int n){
	char *strnum = malloc(128);
	snprintf(strnum, 128, "%d", n);
	char *rev = reverse(strnum);

	if (strcmp(rev, strnum) == 0) { // is the same both ways round
		free(strnum);
		free(rev);
		return true;
	}
	free(strnum);
	free(rev);
	return false;
}

int findhighestsumof2(int sums[][2], int sumcount){
	int highest = sums[0][0];
	int highestIndex = 0;

	for (int i = 0; i < sumcount; i++) {
		if (sums[i][0] > highest) {
			highest= sums[i][0];
			highestIndex = i;
		}
		if (sums[i][1] > highest) {
			highest = sums[i][1];
			highestIndex = i;
		} 
	}	

	return highestIndex;
}

int findlowesetsumof2(int sums[][2], int sumcount){
	if (sumcount == 0) return -1; // no sums
	int lowest = sums[0][0];
	int lowestIndex = 0;
	bool needscheck = false;

	for (int i = 0; i < sumcount; i++) {
		if (sums[i][0] < lowest) {
			lowest = sums[i][0];
			lowestIndex = i;
			needscheck = false;
		}
		if (sums[i][1] < lowest) {
			lowest = sums[i][1];
			lowestIndex = i;
			needscheck = false;
		} 
		if (sums[i][0] == lowest || sums[i][1] == lowest) needscheck == true;
	}
	if (needscheck) return findhighestsumof2(sums, sumcount);
	return lowestIndex;
}

int findhighestsumof3(int sums[][3], int sumcount){
	int highest = sums[0][0];
	int highestIndex = 0;

	for (int i = 0; i < sumcount; i++) {
		if (sums[i][0] > highest) {
			highest= sums[i][0];
			highestIndex = i;
		}
		if (sums[i][1] > highest) {
			highest = sums[i][1];
			highestIndex = i;
		} 
		if (sums[i][2] > highest) {
			highest = sums[i][2];
			highestIndex = i;
		} 
	}	
	return highestIndex;
}

int findlowesetsumof3(int sums[][3], int sumcount){
	if (sumcount == 0) return -1; // no sums
				      //
	int lowest = sums[0][0];
	int lowestIndex = 0;
	bool needscheck = false;

	for (int i = 0; i < sumcount; i++) {
		if (sums[i][0] < lowest) {
			lowest = sums[i][0];
			lowestIndex = i;
			needscheck = false;
		}
		if (sums[i][1] < lowest) {
			lowest = sums[i][1];
			lowestIndex = i;
			needscheck = false;
		} 
		if (sums[i][2] < lowest) {
			lowest = sums[i][2];
			lowestIndex = i;
			needscheck = false;
		}
		if (sums[i][0] == lowest || sums[i][1] == lowest || sums[i][0] == lowest) needscheck == true;
	}
	if (needscheck) return findhighestsumof3(sums, sumcount);
	return lowestIndex;
}


int main(int argc, char **argv){
	int input;
	if (argc == 1) {
		assert(scanf("%d", &input) != 0);
	} else input = atoi(argv[1]);

	if (ispal(input)) { // if 1 number is enough
		printf("%d\n", input);
		return 1;
	}

	int sums[input*2][2];
	int sumcount = 0;
	for (int i = 1; i < input/2+1; i++){ // if 2 is enough
		for (int j = input; j > (input/2) - 1; j--){
			if ((i + j) == input) {
				if (ispal(i) & ispal(j)){
					sums[sumcount][0] = i;
					sums[sumcount][1] = j;
					sumcount++;
				}
			}
		}
	}

	int index = findlowesetsumof2(sums, sumcount);
	if (index != -1) {
		printf("%d %d\n", sums[index][0], sums[index][1]);
		return 2;
	}
	
	int triples[input*3][3];
	int triplecount = 0;
	for (int i = 0; i < input; i++){ // if 3 is enough
		for (int j = 0; j < input; j++){ 
			for (int k = 0; k < input; k++){
				if ((i + j + k) == input) {
					if (ispal(i) & ispal(j) & ispal(k)){
						triples[triplecount][0] = i;
						triples[triplecount][1] = j;
						triples[triplecount][2] = k;
						triplecount++;
					}
				}
			}
		}
	}

	index = findlowesetsumof3(triples, triplecount);
	if (index != -1) {
		printf("%d %d %d\n", triples[index][0], triples[index][1], triples[index][2]);
		return 3;
	}
}