Dhaka 2003 / UVa 12050 - Palindrome Numbers (回文数)

12050 - Palindrome Numbers

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=3202

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name "anna" is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The first few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ...

The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.

Input 

The input consists of a series of lines with each line containing one integer value  i (1 <= i <= 2*109) . This integer value  i  indicates the index of the palindrome number that is to be written to the output, where index  1  stands for the first palindrome number  (1) , index  2  stands for the second palindrome number  (2)  and so on. The input is terminated by a line containing  0 .

Output 

For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value  i  the  i-th  palindrome number is to be written to the output.

Sample Input 

1
12
24
0

Sample Output 

1
33
151

打表,先根据表找出这个回文数有多少位,然后算一般,另一半照前面的输出。


完整代码:

/*0.015s*/

#include<cstdio>
#include<cstring>
#include<cstdlib>
const int endsum[21] =
{
	0, 9, 18, 108, 198, 1098, 1998, 10998, 19998, 109998, 199998, 1099998, 1999998,
	10999998, 19999998, 109999998, 199999998, 1099999998, 1999999998, 2000000001
};

char str[20];

int main(void)
{
	int len, num, i, j, maxj;
	int ans;
	while (gets(str), str[0] & 15)
	{
		len = strlen(str);
		num = atoi(str);
		if (len == 1)
			putchar(str[0]);
		else if (num < 19)
		{
			num += 39;
			putchar(num);putchar(num);
		}
		else
		{
			i = 2;
			while (endsum[++i] < num)
				;
			/// 此时回文数的位数为i
			num -= endsum[i - 1] + 1; /// 多减一个
			/// 现在num是第num个i位数的回文数
			ans = 1;
			maxj = (i + 1) >> 1; /// i/2上取整
			for (j = 1; j < maxj; ++j)
				ans *= 10;
			ans += num;
			sprintf(str, "%d", ans);
			for (j = 0; j < maxj; ++j)
				putchar(str[j]);
			if ((i & 1) == 0)///优先级啊。。
				putchar(str[maxj - 1]);
			for (j = maxj - 2; j >= 0; --j)
				putchar(str[j]);
		}
		putchar('\n');
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
['Asia/Aden', 'Asia/Almaty', 'Asia/Amman', 'Asia/Anadyr', 'Asia/Aqtau', 'Asia/Aqtobe', 'Asia/Ashgabat', 'Asia/Ashkhabad', 'Asia/Atyrau', 'Asia/Baghdad', 'Asia/Bahrain', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Barnaul', 'Asia/Beirut', 'Asia/Bishkek', 'Asia/Brunei', 'Asia/Calcutta', 'Asia/Chita', 'Asia/Choibalsan', 'Asia/Chongqing', 'Asia/Chungking', 'Asia/Colombo', 'Asia/Dacca', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dili', 'Asia/Dubai', 'Asia/Dushanbe', 'Asia/Famagusta', 'Asia/Gaza', 'Asia/Harbin', 'Asia/Hebron', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong', 'Asia/Hovd', 'Asia/Irkutsk', 'Asia/Istanbul', 'Asia/Jakarta', 'Asia/Jayapura', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Kamchatka', 'Asia/Karachi', 'Asia/Kashgar', 'Asia/Kathmandu', 'Asia/Katmandu', 'Asia/Khandyga', 'Asia/Kolkata', 'Asia/Krasnoyarsk', 'Asia/Kuala_Lumpur', 'Asia/Kuching', 'Asia/Kuwait', 'Asia/Macao', 'Asia/Macau', 'Asia/Magadan', 'Asia/Makassar', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia', 'Asia/Novokuznetsk', 'Asia/Novosibirsk', 'Asia/Omsk', 'Asia/Oral', 'Asia/Phnom_Penh', 'Asia/Pontianak', 'Asia/Pyongyang', 'Asia/Qatar', 'Asia/Qostanay', 'Asia/Qyzylorda', 'Asia/Rangoon', 'Asia/Riyadh', 'Asia/Saigon', 'Asia/Sakhalin', 'Asia/Samarkand', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Srednekolymsk', 'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tbilisi', 'Asia/Tehran', 'Asia/Tel_Aviv', 'Asia/Thimbu', 'Asia/Thimphu', 'Asia/Tokyo', 'Asia/Tomsk', 'Asia/Ujung_Pandang', 'Asia/Ulaanbaatar', 'Asia/Ulan_Bator', 'Asia/Urumqi', 'Asia/Ust-Nera', 'Asia/Vientiane', 'Asia/Vladivostok', 'Asia/Yakutsk', 'Asia/Yangon', 'Asia/Yekaterinburg', 'Asia/Yerevan']
02-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值