博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【NOIP1999】【Luogu1015】回文数(高精度,模拟)
阅读量:4556 次
发布时间:2019-06-08

本文共 1611 字,大约阅读时间需要 5 分钟。

problem

  • 给定一个n进制的数m
  • 每次进行如下操作:反转(比如把56变成65)后两数相加,得新数。
  • 求最少经过几步可以得到新数为回文数,超过30步输出Impossible!

solution

  • 判断回文或者反转一个数可以用STL的reverse。
  • 两n进制数相加模拟高精即可(竖式手算

codes

#include
#include
#include
#include
#include
using namespace std;const int maxn = 105;string s="0123456789ABCDEF";string add(int k, string aa){
//k进制,返回"aa"+"反转后的aa" //反转 string bb = aa; reverse(bb.begin(),bb.end()); //转存高精 int a[maxn], b[maxn], c[maxn]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); a[0] = aa.size(); b[0] = a[0]; c[0] = a[0]*2;//c可能进位 for(int i = 0; i < a[0]; i++){ //16进制特判 if(isdigit(aa[i]))a[a[0]-i] = aa[i]-'0'; else a[a[0]-i] = aa[i]-'A'+10; //因为反转,a与b一定是等长的,可以一起算 if(isdigit(bb[i]))b[b[0]-i] = bb[i]-'0'; else b[b[0]-i] = bb[i]-'A'+10; } //模拟加法 int x = 0;//x是进位 for(int i = 1; i <= c[0]; i++){ c[i] = a[i]+b[i]+x; x = c[i]/k; //k进制除k c[i] %= k; //k进制膜k } c[c[0]] = x;//最后一个进位 while(c[0]>1 && c[c[0]]==0)c[0]--;//除0 //返回答案 string ans; for(int i = c[0]; i >= 1; i--)//存的是反序 ans += s[c[i]]; return ans;}bool check(string a){
//判断回文 string b = a; reverse(b.begin(), b.end());//反转字符串 return a==b;}int main(){ int n; string m; cin>>n>>m; for(int i = 1; i <= 30; i++){
//无脑模拟 m = add(n,m);//反转加一下 if(check(m)){
//是回文就输出步数 cout<<"STEP="<
<<'\n'; return 0; } } cout<<"Impossible!"<

转载于:https://www.cnblogs.com/gwj1314/p/9444681.html

你可能感兴趣的文章
《Python学习之路 -- Python基础之迭代器及for循环工作原理》
查看>>
struts2注解方式的验证
查看>>
CS 和 BS 的区别和优缺点
查看>>
(三)配置本地YUM源
查看>>
【LeetCode & 剑指offer刷题】数组题17:Increasing Triplet Subsequence
查看>>
【MySQL】ERROR 1045 (28000): Access denied for user的解决方法
查看>>
centos安装mysql57
查看>>
HDU 2002 计算球体积
查看>>
GROUP BY 与聚合函数 使用注意点
查看>>
oracle表名、字段名大小写问题。
查看>>
SVN学习--VisualSVN Server和TortoiseSVN的配置和使用
查看>>
CSS-继承和层叠
查看>>
「雕爷学编程」Arduino动手做(13)——触摸开关模块
查看>>
【u119】中位数
查看>>
【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses
查看>>
Python Pandas分组聚合
查看>>
Thymeleaf 学习笔记
查看>>
MAC IP等相关
查看>>
Unable to instantiate prefab. Prefab may be broken.(Unity2018.2.2报错)
查看>>
Java中的TreeMap、Comparable、Comparator
查看>>