我的第一篇博文:优快云编程挑战回文数

本文介绍了一种简单有效的回文数检测方法,并提供了一个C语言实现的示例程序。该程序不仅能判断一个数是否为回文数,还能在指定范围内枚举所有回文数。

其实好久前就想写写博客,只是一直苦于不知道写什么,或者这只是一种拖延症的借口,现在就什么都不想,刚好在登录优快云时看到了编程挑战--回文数,想想就先写下这个,作为自己写博客的起步吧。


接下来进入正题,回文数是一类数字的总称,这些数字从前往后看和从后往前看都是一样的,例如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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值