题目描述:
若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数。
例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数。
又如:对于10进制数87:
STEP1:87+78 = 165 STEP2:165+561 = 726
STEP3:726+627 = 1353 STEP4:1353+3531 = 4884
在这里的一步是指进行了一次N进制的加法,上例最少用了4步得到回文数4884。
写一个程序,给定一个N(2<=N<=10,N=16)进制数M(100位之内),求最少经过几步可以得到回文数。如果在30步以内(包含30步)不可能得到回文数,则输出“Impossible!”

输入格式:
两行,分别是N,M。

输出格式:
STEP=ans

输入样例:
10
87
输出样例:
STEP=4

难度:简单


这道题主要考查两点:reverse和check。
将数存成数组的形式(也就是高精度的写法),reverse就很好写(swap一下)。

源代码如下:

  1. /*
  2. About: luogu_P1015_回文数
  3. Auther: kongse_qi
  4. Date:2017/04/21
  5. */
  6. #include <bits/stdc++.h>
  7. #define maxn 10005
  8. using namespace std;
  9. int Base, times;
  10. string n;
  11. struct qi
  12. {
  13. int len, s[maxn], base;
  14. qi ()//初始化结构体
  15. {
  16. memset(s, 0, sizeof s);
  17. len = 0;
  18. base = Base;
  19. }
  20. qi (string a, int b)//也是初始化(用于输入)
  21. {
  22. len = a.size();
  23. for(int i = 0; i < len; i++)
  24. {
  25. if(a[len-1-i] >= 'A')
  26. {
  27. s[i] = a[len-i-1] -'A'+10;
  28. }
  29. else
  30. {
  31. s[i] = a[len-i-1] -'0';
  32. }
  33. }
  34. base = b;
  35. }
  36. qi operator + (const qi &b)//+
  37. {
  38. qi c;
  39. c.len = 0;
  40. for(int i = 0, g = 0; g || i < max(len, b.len); i++)
  41. {
  42. int x = g;
  43. if(i < len) x += s[i];
  44. if(i < b.len) x += b.s[i];
  45. c.s[c.len++] = x % base;
  46. g = x / base;
  47. }
  48. return c;
  49. }
  50. qi operator = (const qi &x)//复制
  51. {
  52. len = x.len;
  53. for(unsigned i = 0; i != len; ++i)
  54. {
  55. s[i] = x.s[i];
  56. }
  57. base = x.base;
  58. }
  59. qi operator += (const qi &x)//就是做上面写好的"+"
  60. {
  61. *this = *this+x;
  62. return *this;
  63. }
  64. void reverse()//反转
  65. {
  66. for(unsigned i = 0; i != len/2; ++i)
  67. {
  68. swap(s[i], s[len-1-i]);
  69. }
  70. return ;
  71. }
  72. }x,y;
  73. void Init()
  74. {
  75. scanf("%d", &Base);
  76. cin >> n;
  77. x = qi(n, Base);
  78. return ;
  79. }
  80. bool check(const qi &a)
  81. {
  82. for(unsigned i = 0; i != a.len/2; ++i)
  83. {
  84. if(a.s[i] != a.s[a.len-i-1]) return false;
  85. }
  86. return true;
  87. }
  88. void Add()
  89. {
  90. do
  91. {
  92. y = x;//copy
  93. x.reverse();//结构体函数
  94. x += y;
  95. ++times;
  96. }
  97. while(!check(x) && times < 31);
  98. if(times < 31)
  99. {
  100. cout << "STEP=" << times;
  101. }
  102. else
  103. {
  104. cout <<"Impossible!";
  105. }
  106. return ;
  107. }
  108. int main()
  109. {
  110. Init();
  111. Add();
  112. return 0;
  113. }

高精度模板

自此完成。
箜瑟_qi 2017.04.21 12:17

luogu P1015 回文数的更多相关文章

  1. 洛谷 P1015 回文数 Label:续命模拟QAQ

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  2. P1015回文数

    传送 回文数的判断有个神奇的公式: g[i]==g[leng+-i] 其中leng为字符串长度,看每个g[i]是否都满足它,若满足,就是回文数 ps:洛谷的impossible有毒,必须得复制题干中的 ...

  3. Java实现 洛谷 P1015 回文数(N进制回文数)

    输入输出样例 输入样例#1: 10 87 输出样例#1: STEP=4 import java.util.Scanner; public class 回文数2 { public static void ...

  4. 洛谷 P1015 回文数

    #include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...

  5. P1015 回文数解题思路(非原创)

    测试 #include<bits/stdc++.h> using namespace std; int n,m,step; string nn; int len,nex; bool dfs ...

  6. 【洛谷p1015】【一本通p1309】回文数(noip1999)

    (过了这个题灰常灰常开心) 好像前两道忘记了传送门: 回文数[传送门] 洛谷算法标签: 其实还有高精度 这个题困死在了十六进制,后来想了想,我们在c[i]中存入一个大于十的数之前的程序也可以实现回文( ...

  7. P1015 [NOIP1999 普及组] 回文数

    点击查看题目 题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个十进制数 5656,将 5656 加 6565(即把 5656 从右向左读),得到 ...

  8. 洛谷—— P1609 最小回文数

    https://www.luogu.org/problemnew/show/1609 题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. ...

  9. hdu1282回文数猜想

    Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...

随机推荐

  1. rpm包相关操作

    1.查找已安装的rpm:rpm -qa|grep ewp2.卸载已安装的rpm: 先切换到虚拟机共享路径,执行卸载命令: rpm -e 已安装rpm包名称3.安装新rpm包:rpm -ivh(更新的话 ...

  2. select count(*)优化 快速得到总记录数

    1.select count(*) from table_name 比select count(主键列) from table_name和select count(1) from table_name ...

  3. Cesium原理篇:3D Tiles(3)个人总结

    个人结论:目前,在演示层面,3D Tiles问题不大,但项目应用上就不够成熟了,所以问问自己,你是想吃瓜呢还是想吃螃蟹? 好的方面 数据规范 我非常喜欢glTF的整体设计,概括有四点:第一,数据块(B ...

  4. Ubuntu14.04上安装openGL

    安装命令:sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev sudo apt-get install ...

  5. 初识 Javascript.02 -- Date日期、Math对象、数据类型转换、字符串、布尔Boolean、逻辑运算符、if else 、三元表达式、代码调试方法、

    Date()对象: Date对象用于处理日期和时间. 1.1 Math对象  ◆Math.ceil()   天花板函数    向上取整  只取整数,不足则进1 ◆Math.floor()  地板函数 ...

  6. vue学习笔记 样式 class style(五)

    使用v-bind数据绑定class和style,v-bind:class可以与传统的class属性共存,其中可以用{}设置多个class,根据条件判断的语法是class名:条件,带-的class名需要 ...

  7. 一行命令创建 http-server

    一行命令启动http-server总结:1. python2.xpython2 -m SimpleHTTPServer 8000 2. python3.xpython -m http.server 8 ...

  8. signedCookies

    var express = require('../') , request = require('supertest') , cookieParser = require('cookie-parse ...

  9. JSON与JAVA的数据转换

    http://developer.51cto.com/art/200906/129090.htm java.lang.ClassNotFoundException: net.sf.json.JSONA ...

  10. 使用Docker容器来源码编译etcd

    背景 etcd是CoreOS公司开发的分布式键值对存储库.在Kubernetes中,我们需要使用etcd作为所有REST API对象的持久化存储. 不幸的是,在github的release中,Core ...