Add problem-145
This commit is contained in:
54
problem-145.c
Normal file
54
problem-145.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_NUM 1000000000
|
||||||
|
|
||||||
|
|
||||||
|
int reverse(int num)
|
||||||
|
{
|
||||||
|
int rem, sum = 0;
|
||||||
|
|
||||||
|
while (num > 0) {
|
||||||
|
rem = num % 10;
|
||||||
|
sum = (sum * 10) + rem;
|
||||||
|
num /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int is_reversible(int num)
|
||||||
|
{
|
||||||
|
int rev, rem, total;
|
||||||
|
|
||||||
|
if (!(num % 10))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
rev = reverse(num);
|
||||||
|
total = num + rev;
|
||||||
|
|
||||||
|
while (total > 0) {
|
||||||
|
rem = total % 10;
|
||||||
|
if (!(rem % 2))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
total /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
int i, count = 0;
|
||||||
|
|
||||||
|
for(i=1; i<MAX_NUM; i+=2)
|
||||||
|
if (is_reversible(i))
|
||||||
|
count++;
|
||||||
|
|
||||||
|
setlocale(LC_NUMERIC, "");
|
||||||
|
printf("There are %'d reversible numbers less than %'d\n", count*2, MAX_NUM);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user