summaryrefslogtreecommitdiff
path: root/euler112.c
diff options
context:
space:
mode:
Diffstat (limited to 'euler112.c')
-rw-r--r--euler112.c56
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;
+ }
+ }
+}
+