Add solution for problem 112
This commit is contained in:
61
problem-112.c
Normal file
61
problem-112.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define TRUE 1
|
||||||
|
#define FALSE 0
|
||||||
|
|
||||||
|
int is_increasing(long num)
|
||||||
|
{
|
||||||
|
int digit;
|
||||||
|
int prev = num % 10;
|
||||||
|
num /= 10;
|
||||||
|
|
||||||
|
while (num>0) {
|
||||||
|
digit = num % 10;
|
||||||
|
if (digit>prev)
|
||||||
|
return FALSE;
|
||||||
|
num /= 10;
|
||||||
|
prev = digit;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int is_decreasing(long num)
|
||||||
|
{
|
||||||
|
int digit;
|
||||||
|
int prev = num % 10;
|
||||||
|
num /= 10;
|
||||||
|
|
||||||
|
while (num>0) {
|
||||||
|
digit = num % 10;
|
||||||
|
if (digit<prev)
|
||||||
|
return FALSE;
|
||||||
|
num /= 10;
|
||||||
|
prev = digit;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int is_bouncy(long num)
|
||||||
|
{
|
||||||
|
if (is_increasing(num) || is_decreasing(num))
|
||||||
|
return FALSE;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
int i = 99;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while((count*100) != (i*99)) {
|
||||||
|
i++;
|
||||||
|
if (is_bouncy(i))
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("The solution is %d / %d = %.9f%%\n", count, i, (double)count/(double)i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user