summaryrefslogtreecommitdiff
path: root/euler112.c
blob: e2414bde3da9413715c0ad7d1db2348e00f8bc42 (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
#include <stdio.h>
#include <assert.h>

int isbouncy(int a)
{
	int d[2];
	int incr;
	if (a < 100)
		return 0;

	d[0] = a % 10;
	d[1] = (a / 10) % 10;
	a /= 100;
	if (d[1] > d[0])
		incr = 1;
	else if (d[1] < d[0])
		incr = -1;
	else
		incr = 0;

	while (a > 0) {
		d[0] = d[1];
		d[1] = a % 10;
		if (incr == 1 && d[1] < d[0])
			return 1;
		if (incr == -1 && d[1] > d[0])
			return 1;
		if (incr == 0) {
			if (d[1] > d[0])
				incr = 1;
			else if (d[1] < d[0])
				incr = -1;
		}
		a /= 10;
	}
	return 0;
}

int main()
{
	assert(isbouncy(66420) == 0);
	assert(isbouncy(134468) == 0);
	assert(isbouncy(155349) == 1);

	int nbouncy = 0;
	for (int i = 100; i <= 0x7fffffff; i++) {
		if (isbouncy(i))
			nbouncy++;

		if (nbouncy * 100 / 99 == i) {
			printf("%d\n", i);
			break;
		}
	}
}