Palindrome Numbers UVALive - 2889

我们可以通过给定的n来判断此回文串的长度,和其与该长度最小的值的差值,我们只需得到回文串的前半部分就可以了。

1位 9  

2位 9

3位 9*10

4位 9*10

5位 9*10*10

.。。。。。

例如 n=50 该回文串长度为3  50-18=32,它的前半部分为10+32-1=41  所以该回文串为414

详见代码。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
const int INF=1e9+10;
const double EPS = 1e-10;  
const ll mod=1e9+7;
typedef pair<int,int> P;
 

int main(){
	//freopen("out.txt","w",stdout);
	//ios_base::sync_with_stdio(0);
	int n;
	while(scanf("%d",&n)&&n){
		int cnt=1;
		ll tmp=1;
		for(;;cnt++){
			if(cnt&1){
				tmp*=1LL*10;
				if(cnt==1) tmp--;
				if(n-tmp<=0) break; 
			}else{
				if(n-tmp<=0) break;
			}
			n-=tmp;	
		}
		int cnt1=(cnt+1)/2;
		ll sum=1;
		for(int i=0;i<cnt1-1;i++)
			sum*=10;
		sum+=n-1;

		printf("%lld",sum );
		if(cnt&1)
			sum/=10;
		while(sum){
			printf("%lld",sum%10 );
			sum/=10;
		}
		printf("\n");
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值