题目

HihoCoder - 1878

题目大意

给出k,让求出第k个回文数(k的“长度”不超过1e5)

题解

之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法。

打表找规律:

只有一位数:减一输出

否则:

若第0位为2~9 :首位减一,0~len-2反转贴后面

若第0位为1:若第1位为1~9:丢掉首位,剩下的反转贴后面

若第1位为0:丢掉首位,第1位改成9,2~len-2反转贴后面.

AC代码

#include<cstdio>
#include<iostream>
#include<string>
#include<sstream>
using namespace std; void solve(string str)
{
int len = str.length(); if (len == )
{
printf("%c\n", str[] - );
return;
} if (str[] == '')
{
if (str[] == '')
{
str[] = '';
for (int i = ; i < len; i++) printf("%c", str[i]);
for (int i = len - ; i >= ; i--) printf("%c", str[i]);
printf("\n");
}
else
{
for (int i = ; i < len; i++) printf("%c", str[i]);
for (int i = len - ; i >= ; i--) printf("%c", str[i]);
printf("\n");
}
}
else
{
str[] = str[] - ;
for (int i = ; i < len; i++) printf("%c", str[i]);
for (int i = len - ; i >= ; i--) printf("%c", str[i]);
printf("\n");
}
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
string str;
cin >> str;
solve(str);
}
}

打表程序

#include<cstdio>
#include<iostream>
#include<sstream>
#include<string>
using namespace std; typedef long long LL; const LL maxn = 1e18; void ToString(LL num, string& str)
{
stringstream ss;
ss << num;
ss >> str;
} bool ispd(string str)
{
bool flag = true;
int len = str.length();
for (int i = ; i < len / ; i++)
if (str[i] != str[len - - i]) flag = false;
return flag;
} int main()
{
LL cnt = ;
for (LL i = ; i < maxn; i++)
{
string s;
ToString(i, s);
if (ispd(s)) cout << cnt++ << ' ' << i << endl;
}
return ;
}

参考链接:https://blog.csdn.net/hxxjxw/article/details/84205963

最新文章

  1. 利用Ajax+MSMQ(消息队列)+WebService实现服务器端向客户端的信息推送
  2. Cocos2d-x 3.x 错误 cocos2dxDownloader 编译报错
  3. js面向对象的实现(example 一)
  4. 【Python】使用 boto 调用 S3 对象存储API
  5. Java 最简单的计算器——使用Args参数
  6. JAVA与.NET的相互调用——通过Web服务实现相互调用
  7. docNet基础学完感想
  8. oracle10g安装图解(win7)
  9. Android 经验: 5555 端口会被 adb 误认为 emulator
  10. App IM 之 环信
  11. matlab中小技巧
  12. c++(爬楼梯)
  13. H5新特性之data-*
  14. 哈希表概念和实现,C/C++实现
  15. Cracking The Coding Interview 9.3
  16. Oracle的基本数据类型(常用)
  17. 前端技术-js插件
  18. Java线程:新特征-有返回值的线程《转》
  19. java代码-----实现4个人打牌游戏的相关代码。线程
  20. [转] FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介

热门文章

  1. 【网络爬虫】【java】微博爬虫(一):小试牛刀——网易微博爬虫(自定义关键字爬取微博数据)(附软件源码)
  2. [Xcode 实际操作]一、博主领进门-(7)使用不同类型的iOS模拟器
  3. 用EnumMap代替序数索引
  4. 在虚拟机中设置NAT模式实现主机和虚拟机的通信
  5. (转)cookie和session的区别
  6. SpringBoot | Data Access
  7. 普通平衡树与文艺平衡树的splay代码
  8. Web Scalability for Startup Engineers Tip&amp;Techniques for Scaling You Web Application --读书笔记
  9. C++使用ADO连接数据库及其实例
  10. 一些JS基本小内容