其实好久前就想写写博客,只是一直苦于不知道写什么,或者这只是一种拖延症的借口,现在就什么都不想,刚好在登录优快云时看到了编程挑战--回文数,想想就先写下这个,作为自己写博客的起步吧。
接下来进入正题,回文数是一类数字的总称,这些数字从前往后看和从后往前看都是一样的,例如1221,2112,1001。。。
依据回文数性质,代码实现起来还是蛮简单的:
#include <stdio.h>
#include <sys/time.h>
int is_palindromic_number(int num)
{
if (num < 0)
return 0;
int copy_of_num = num;
int convert_of_num = 0;
while (copy_of_num > 0) {
convert_of_num = convert_of_num * 10 + copy_of_num % 10;
copy_of_num /= 10;
}
if (convert_of_num == num)
return 1;
return 0;
}
int main(int args, char** argc)
{
int start;
int end;
scanf("%d %d", &start, &end);
if (start < 0 || end < 0)
return 0;
//exchange the value of start and end
if (start > end) {
start ^= end;
end ^= start;
start ^= end;
}
#ifdef DEBUG
struct timeval start_timestamp;
gettimeofday(&start_timestamp, NULL);
#endif
int i;
for (i = start; i < end; i++)
if (is_palindromic_number(i))
printf("%d\n", i);
#ifdef DEBUG
struct timeval end_timestamp;
gettimeofday(&end_timestamp, NULL);
printf("elapsed time: %d\n",
(end_timestamp.tv_sec - start_timestamp.tv_sec)*1000
+ (end_timestamp.tv_usec - start_timestamp.tv_usec) / 1000
);
#endif
return 0;
}