From 9920a766c17402a58fc4457d2fc643cc7905e684 Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Mon, 11 Jun 2018 16:46:43 +0800 Subject: 112, 345-347 --- euler112.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 euler112.c (limited to 'euler112.c') diff --git a/euler112.c b/euler112.c new file mode 100644 index 0000000..e2414bd --- /dev/null +++ b/euler112.c @@ -0,0 +1,56 @@ +#include +#include + +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; + } + } +} + -- cgit v1.2.3