SPOJ:The Next Palindrome(贪心&思维)
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133 Output:
818
2222
Warning: large Input/Output data, be careful with certain languages
题意:输出比X大的第一个回文字符串。
思路:先把X按左半边为标准变成一个回文串X2,如果X2大于X,则输出X2。 否则变大X2 :
如果X2全部为9,则需要加一位,变为首尾为‘1’,之间为‘0’的回文串。
否则,从之间开始找第一位非‘9’的位置,自加1。然后中间取余变为‘0’。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char c[maxn],c2[maxn];
int T,N,Len,a[maxn];
bool check9()
{
for(int i=;i<=Len;i++)
if(c[i]!='') return false;
return true;
}
void Tochange()
{
for(int i=;i<=Len/;i++) c2[i]=c[i];
for(int i=Len/+;i<=Len;i++) c2[i]=c[Len+-i];
}
bool Toupper()
{
for(int i=;i<=Len;i++)
if(c2[i]>c[i]) return true;
else if(c2[i]<c[i]) return false;
return false;
}
int main()
{
int i,j;
scanf("%d",&T);
while(T--){
scanf("%s",c+);
Len=strlen(c+);
Tochange();
if(Toupper()) {
for(i=;i<=Len;i++) putchar(c2[i]);
cout<<endl;
continue;
}
if(check9()) {
putchar('');
for(i=;i<Len;i++) putchar('');
putchar('');
cout<<endl;
continue;
}
int np,Mid;
if(Len&) Mid=(Len+)/;
else Mid=Len/;
for(np=Mid;np>=;np--) if(c[np]!='') break;
c[np]++;
for(i=np+;i<=Mid;i++) c[i]='';
for(i=;i<=Mid;i++) putchar(c[i]);
for(i=Len/;i>=;i--) putchar(c[i]);
cout<<endl;
}
return ;
}
SPOJ:The Next Palindrome(贪心&思维)的更多相关文章
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
- C. Coffee Break 贪心 思维 有点难 有意思
C. Coffee Break 这个贪心之前好像写过,还是感觉挺难的,有点不会写. 这个题目大意是:给你一个数列n个元素,然后给你一天的时间,给你一个间隔时间d, 问你最少要用多少天可以把这个数列的所 ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
随机推荐
- python005 Python3 注释
Python3 注释确保对模块, 函数, 方法和行内注释使用正确的风格Python中的注释有单行注释和多行注释:Python中单行注释以 # 开头,例如:: # 这是一个注释 print(" ...
- Personal Recommendation Using Deep Recurrent Neural Networks in NetEase读书笔记
一.文章综述 1.研究目的:实现网易考拉电商平台的商品高效实时个性化推荐.缩短用户与目标商品的距离,让用户点击最少的页面就可以得到想要的商品 2.研究背景:基于用户和基于物品的协同过滤(Collabo ...
- hdu 1232水
#include<stdio.h> #define N 1000 int pre[N]; int find(int n ){ return pre[n]=n==pre[n]?n:find( ...
- BZOJ1710: [Usaco2007 Open]Cheappal 廉价回文
len<=2000的字符串上,给出删掉和添加每种字符的花费,求把字符串变成回文串的最小花费. 首先每个字符添加和删除是一样的,因此花费在添加和删掉每个字符的花费中取小的. 如果每个字符的花费都是 ...
- msp430入门编程34
msp430中C语言的可移植--消除硬件差异
- OC-runtime 的温习
-.runtime简介 runtime简称运行时,OC就是运行时机制,也就是运行时的一些机制,其中最主要的是消息机制: 对于C语言,函数的调用在编辑的时候,会决定调用哪个函数: 对于OC的函数,属于动 ...
- HDU 6441 费马大定理+勾股数
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se ...
- Codeforces 920G(二分+容斥)
题意: 定义F(x,p)表示的是一个数列{y},其中gcd(y,p)=1且y>x 给出x,p,k,求出F(x,p)的第k项 x,p,k<=10^6 分析: 很容易想到先二分,再做差 然后问 ...
- Office EXCEL 中单元格怎么打斜线
右击单元格,然后设置单元格格式,然后添加需要的边框 注意里面的文字有讲究,比如我要右上角显示Value,左下角显示Payload,则需要先输一堆空格,然后输入Value,把Value挤到右边去 ...
- 阅读《Android 从入门到精通》(33)——Intent 分类
Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...