diff options
author | Iru Cai <mytbk920423@gmail.com> | 2018-06-11 16:46:43 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2018-06-11 16:46:43 +0800 |
commit | 9920a766c17402a58fc4457d2fc643cc7905e684 (patch) | |
tree | b966224ecba3cefbd11a74ad1f996dd1e35ee115 /euler112.c | |
parent | 7f25c6fc31b376ca1ed0f4c183ef4d232b6ee2b5 (diff) | |
download | project_euler-9920a766c17402a58fc4457d2fc643cc7905e684.tar.xz |
112, 345-347
Diffstat (limited to 'euler112.c')
-rw-r--r-- | euler112.c | 56 |
1 files changed, 56 insertions, 0 deletions
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 <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; + } + } +} + |