zoj2000 Palindrome Numbers

Palindrome Numbers


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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*10^9 ). 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

题意:求第i个回文数字;

解析:

位数为1的回文个数:9

位数为2的回文个数:9

位数为3的回文个数:90

位数为4的回文个数:90

位数为5的回文个数:900

位数为6的回文个数:900

位数为7的回文个数:9000

位数为8的回文个数:90000

.……

位数为n的回文个数:9*(10^((n-1)/2)),

将这些回文数拆分一半,可以发现

---------------------------------------------------------1位

1 2
3 4 5
6 7
8 9

---------------------------------------------------------2位

1 2
3 4 5
6 7
8 9

---------------------------------------------------------3位

10 11
12 13 14
15 16
17 18 19

20 21
22 23 24
25 26
27 28 29

……

90 91
92 93 94
95 96
97 98 99

----------------------------------------------------------4位

10 11
12 13 14
15 16
17 18 19

20 21
22 23 24
25 26
27 28 29

……

90 91
92 93 94
95 96
97 98 99

---------------------------------------------------------5位

100 101
102 103
104 105
106 107
108 109

110 111
112 113
114 115
116 117
118 119

……

990 991
992 993
994 995
996 997
998 999

---------------------------------------------------------6位

……

参考代码:

#include <iostream>
using namespace std;
typedef long long ll;
ll pow(int a,int b){
	ll ans = 1;
	for (int i=0;i<b;i++){
		ans*=a;
	}
	return ans;
}
int main(){
	ll n;
	while (cin>>n&&n){
		int count=1;
		int q=9;
		while (n>q){
			n-=q;
			count++;
			if (n>q){
				n-=q;
				count++;
			}
			else
				break;
			q*=10;
		}
		//cout<<"n="<<n<<endl;
		int k=(count-1)/2;
		//cout<<"k="<<k<<endl;
		ll ans=pow(10,k)+n-1;
		//cout<<"ans="<<ans<<endl;
		//cout<<"count="<<count<<endl;
		int a[20],s=0;
		cout<<ans;
		while (ans){
			a[s++]=ans%10;
			ans/=10;
		}
		for (int j=count/2-1;j>=0;j--){
			if (j<0)
				break;
			cout<<a[s-1-j];
		}
		cout<<endl;
	}
	return 0;
}

参考代码:

时间: 2024-10-13 17:06:01

zoj2000 Palindrome Numbers的相关文章

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5

POJ2402/UVA 12050 Palindrome Numbers 数学思维

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). Additionallynumbers can of course be ordered in size. The ?rst few

Palindrome Numbers(LA2889)第n个回文数是?

 J - Palindrome Numbers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 2889 WA了一版面,我也是醉了,就因为一个编译环境不同...... 说多了都是泪. 题目链接:请点击UVALive 2889 转载请注明出处:寻找&星空の孩子 #include<stdio.h> #define LL lo

E - Palindrome Numbers

题目链接:https://vjudge.net/contest/237394#problem/E 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). Additionall

Leetcode: Palindrome Numbers

尝试用两头分别比较的方法,结果发现无法解决1000021这种问题 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x<0) return false; 4 int lastbit=x%10; 5 int firstbit; 6 int num=x; 7 int bits=0; 8 while(num/10!=0){ 9 num=num/10; 10 bits++; 11 } 12 firstbit=num

UVA 12050 - Palindrome Numbers 模拟

题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; for(sum=i=1;i<=x;i++) sum *= 10; return sum; } int main() { int n, i, j, t, cs[1000], c; while(~scanf("%d", &n)) { if(n==0) break; i=1; while(n>9*

Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 662    Accepted Submission(s): 351 Problem Description As we all know,a palindrome number is the number which reads the same backward as forward

UVA - 12050-Palindrome Numbers

12050 - Palindrome Numbers Time limit: 3.000 seconds 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). Additio

hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

Beautiful Palindrome Number                                                                 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 116    Accepted Submission(s): 82 Problem Description