UVA 12050
UVA 12050
题意是求第n个回文书,0<n<2e10 (n为整数)
没做出来,赛后看完答案才知道,这个是有规律的。长度为n的回文数有9×10(n/2)个,假设长度为3,在长度为n的第1个是101,第2个是111……第77个为867,写了很多后就知道规律了,就不多说,看AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 int a[3000],n; 5 ll num[20]; 6 void init(){ 7 num[0] = 0; num[1]=num[2] = 9; 8 for(int i = 3; i < 20; i+=2){ 9 num[i] = num[i+1] = num[i-1]*10; 10 } 11 } 12 int main(){ 13 init(); 14 while(scanf("%d",&n)!=EOF&&n){ 15 int len = 1; 16 while(n > num[len]){ 17 n -= num[len]; 18 len++; 19 } 20 n--; 21 int cnt = len/2 + 1; 22 while(n){ 23 a[cnt++] = n%10; 24 n/=10; 25 } 26 for(int i = cnt; i <= len; i ++) 27 a[i] = 0; 28 a[len]++; 29 for(int i = 1; i <= len/2; i ++) 30 a[i] = a[len-i+1]; 31 for(int i = 1; i <= len; i ++) 32 printf("%d",a[i]); 33 printf("\n"); 34 } 35 return 0; 36 }