题目

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 10^10) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3


题解

#include<iostream>
#include<algorithm>
using namespace std;
string s;void add(string t){int len=s.length();int flag=0;for(int i=len-1;i>=0;i--){//这边循环我把末位作为最低位s[i]=(s[i]-'0')+(t[i]-'0')+flag+'0';//得到字符(这边柳神的博客简化了,我扩充回来)flag=0;if(s[i]>'9'){s[i]=s[i]-10;//ASCII码内部转换为int,与10相减flag=1;}}if(flag) s='1'+s;//最后一位有进位,字符串前面+‘1’}
int main(){int cnt,i;cin>>s>>cnt;for(i=0;i<cnt;i++){//这边的i不能重新定义!!否则输出i为随机数string t=s;reverse(t.begin(),t.end());if(s==t||i==cnt) break;add(t);}cout<<s<<endl<<i;return 0;
}

这题的矛盾点主要在,我以为s[i]超出‘9’后,应该减17得到个位数。经测试。‘7’+‘6’输出了‘=’ ,而不是D。

‘=’的ASCII码为13,变为3。因此还是减去10。

PAT甲级1024 ASCII码与整数转换相关推荐

  1. PAT甲级1024 Palindromic Number:[C++题解]回文串和高精度加法

    文章目录 题目分析 题目链接 题目分析 一个判断是否是回文数的函数:check,思路是使用双指针从两端分别往里走. 另一个是高精度加法函数add,传入两个vector. 另外,vector逆序构造可以 ...

  2. 字母与ASCII码之间的转换

    1.先解释下两个名词,主要从百度百科中查询得到: ASCII (American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字 ...

  3. c#字符型转化为asc_C#中使用强制类型实现字符串和ASCII码之间的转换

    C#貌似没有专门用于ASCII码转字符或字符转ASCII码的系统函数,所以小编这里就借用一下强制类型转换来实现ASCII码与字符之间的互转. 所谓的ASCII码,即American Standard ...

  4. c# 字符串与16进制ASCII码相到转换

    1.普通字符串转16进制ASCII码 //普通字符串转16进制ASCII码 public static string toASCII(string code){char[] cs = code.ToC ...

  5. 【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)

    题干: A number that will be the same when it is written forwards or backwards is known as a Palindromi ...

  6. ASCII码的大小写转换

    大小写之间只差一个0x20 // 转大写 char ASCII_2Big(char dat) {return dat& (~0x20); }// 转小写 char ASCII_2Small(c ...

  7. 【PAT甲级】字符串处理及进制转换专题

    目录 字符串处理 PAT甲级 1001 A+B Format (20 分) PAT甲级1005 Spell It Right (20 分) PAT甲级1035 Password (20 分) PAT甲 ...

  8. Java中文与ASCII码的转换

    今天在研究Java中编码的时候,看到了Java中ascii码的强大.写了一个CoderUtils.java,以后会扩展它. package com.xingxd.study.test; import ...

  9. Unicode码和ASCII码及其转换

    关于什么是编码,我在之前的文章简单介绍过了,这里我们来看一下Unicode码和ASCII码 Unicode码 Unicode码:Unicode码是一种国际标准编码,采用二个字节编码,与ASCII码不兼 ...

最新文章

  1. Mat对象与它各种用法
  2. 基于Python的颜色识别器
  3. python取均匀不重复的随机数
  4. boost::geometry::model::segment用法的测试程序
  5. live555 源码分析:MediaSever
  6. python中排序从小到大_从Python看排序:冒泡排序
  7. [转]迭代、集合、字典表和列表
  8. html video 样式修改,修改video样式代码
  9. cad通过钢筋大样生成钢筋明细表插件_各位做室内外设计的朋友,告别CAD单线画图,用天正建筑更方便...
  10. 守得云开见月明:一次ASM存储高可用故障解决过程分析
  11. 输出毫秒_毫秒级网络监控(网络示波器)
  12. Premiere无法导入webm格式视频的解决方法
  13. 谷歌Adblock Plus 广告拦截插件-屏蔽百度热搜和推荐(附网盘下载地址)
  14. 使用Python+TensorFlow2构建基于卷积神经网络(CNN)的ECG心电信号识别分类(二)
  15. FastAPI中Jinjia2使用
  16. 读书笔记(十二)--穷爸爸,富爸爸
  17. 二十一世纪大学英语读写教程(第三册)学习笔记(原文)——10 - Plain Talk About Handling Stress(浅谈如何缓解压力)
  18. 【CTR预估】criteo数据集预处理shell命令
  19. JSP--9大隐式对象
  20. (转)图文版本全面讲解电脑主板

热门文章

  1. keil5怎么接入汇编_keil中如何让汇编语言生成hex文件
  2. 软件使用方法_视频录制软件进行电脑屏幕录像的使用方法
  3. 在线mod计算机,计算机系中有关mod的常识(全).doc
  4. android多板面式布局,Android Design
  5. c access mysql数据库_基于C#的Access MsSQL MySQL 三种数据库访问演示(含源文件Demo)...
  6. AdneneBoumessouer / MVTec-Anomaly-Detection学习笔记
  7. 关于USES_CONVERSION宏
  8. Python Imaging Library: ImagePalette Module(图像调色板模块)
  9. 天价部队到老家赶来java作文_天价与廉价作文800字
  10. 我是 LinkedIn 的 SRE ,我把 LinkedIn 搞挂了