UVALive 2889(回文数字)

时间:2023-02-15 06:35:24

题意:给定i,输出第i个回文数字。

分析:1,2,3,4,……,9------------------------------------------------------------------------------------------9个

   11,12,13,14,……,19-------------------------------------------------------------------------------------9个

   101,111,121,131,141,151,161,171,181,191,202,212,222,232,……,979,989,999-------------------------------90个

   1001,1111,1221,1331,1441,……,9889,9999----------------------------------------------------------------90个

   10001,10101,10201,10301,……,99899,99999--------------------------------------------------------------900个

   规律是把回文串一分为二看,例如第四行,前两个数字是从10到99,共90个数字。而第三行也是从10到99,区别在于,需要去掉最后一位再反转,才是另一半(后两个数字)。

思路:先确定i所对应的回文数字的位数,再确定具体值。

 #include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
#define pr(x) cout << #x << " : " << x << " "
#define prln(x) cout << #x << " : " << x << endl
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-;
const int dx[] = {, , -, };
const int dy[] = {-, , , };
const ll MOD = 1e9 + ;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
ll a[];
void init()
{
ll tmp = ll();
for(int i = ; i < ; i += )
{
a[i] = a[i - ] = tmp;
tmp *= ll();
}
}
ll POW(ll x)
{
ll w = ;
for(ll i = ; i <= x; ++i)
w *= ll();
return w;
}
int main()
{
init();
int n;
while(scanf("%d", &n) == && n)
{
int cnt = ;
while(n > a[cnt])
{
n -= a[cnt];
++cnt;
}
if(cnt == )
{
printf("%d\n", n);
continue;
}
else if(cnt == )
{
printf("%d%d\n", n, n);
continue;
}
else
{
int tmp = cnt / ;
char str[];
memset(str, , sizeof str);
ll ans = POW(ll(cnt / )) + ll(n - );
sprintf(str, "%lld", ans);//把数字变为指定格式的字符串
printf("%s", str);
string s = string(str);
int len = s.size();
if(cnt % == ) s.resize(len - );
reverse(s.begin(), s.end());
printf("%s\n", s.c_str());
}
}
return ;
}