LA-2889(dp)
2016-07-07 14:19
381 查看
题意:
给定一个数n,问第n个回文数是多少;
思路:
回文数可以奇数长度可以偶数长度;比如长度为1和长度为2的个数是一样的;然后就减减就好了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1203;
const int maxn=1005;
LL dp[100];
int ans[200];
void Init()
{
dp[2]=dp[1]=9;
for(int i=3;i<100;i+=2)
dp[i]=dp[i+1]=dp[i-1]*10;
}
LL solve(LL x)
{
int d;
for( d=1;d<100;d++)
{
if(x>dp[d])x-=dp[d];
else break;
}
LL w=1;
for(int i=1;i<d;i++)
{
if(i%2==0)w*=10;
}
w+=x-1;
int cnt=0;
while(w)
{
ans[++cnt]=w%10;
w/=10;
}
for(int i=cnt;i>0;i--)printf("%d",ans[i]);
for(int i=1+(d&1);i<=cnt;i++)printf("%d",ans[i]);
printf("\n");
}
int main()
{
LL n;
Init();
while(1)
{
cin>>n;
if(!n)break;
solve(n);
}
return 0;
}
给定一个数n,问第n个回文数是多少;
思路:
回文数可以奇数长度可以偶数长度;比如长度为1和长度为2的个数是一样的;然后就减减就好了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1203;
const int maxn=1005;
LL dp[100];
int ans[200];
void Init()
{
dp[2]=dp[1]=9;
for(int i=3;i<100;i+=2)
dp[i]=dp[i+1]=dp[i-1]*10;
}
LL solve(LL x)
{
int d;
for( d=1;d<100;d++)
{
if(x>dp[d])x-=dp[d];
else break;
}
LL w=1;
for(int i=1;i<d;i++)
{
if(i%2==0)w*=10;
}
w+=x-1;
int cnt=0;
while(w)
{
ans[++cnt]=w%10;
w/=10;
}
for(int i=cnt;i>0;i--)printf("%d",ans[i]);
for(int i=1+(d&1);i<=cnt;i++)printf("%d",ans[i]);
printf("\n");
}
int main()
{
LL n;
Init();
while(1)
{
cin>>n;
if(!n)break;
solve(n);
}
return 0;
}
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes