返回

1024 Palindromic Number (25 分)

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

注意大整数乘法是bign和int以及while

大整数加法是bign和bign以及if

#include <cstdio>
#include <algorithm>#include <cstring>
using namespace std;struct bign{int d[100];int len;bign(){memset(d,0,sizeof(d));len=0;}
};//下面将整数转换成bign,逆序存放
bign change(char str[]){bign a;a.len=strlen(str);for(int i=0;i<a.len;i++){a.d[i]=str[a.len-i-1]-'0';}return a;
}//下面为高精度乘法bign add(bign a,bign b){bign c;int carry=0;//carry为进位for(int i=0;i<a.len||i<b.len;i++){int temp=a.d[i]+b.d[i]+carry;c.d[c.len++]=temp%10;carry=temp/10;}if(carry!=0){//注意这里时ifc.d[c.len++]=carry;}return c;
}bool judge(bign a){for(int i=0;i<a.len/2;i++){if(a.d[i]!=a.d[a.len-1-i])return false;}return true;
}void print(bign a){for(int i=a.len-1;i>=0;i--){printf("%d",a.d[i]);}printf("\n");
}int main(){char str[100];int t;//操作次数的上限int k=0;scanf("%s %d",str,&t);bign a=change(str);for(int i=0;i<t;i++){if(judge(a)==true){print(a);printf("%d\n",k);return 0;}//注意输入的str就是回文这个点bign b=a;reverse(b.d,b.d+b.len);a=add(a,b);k++;}print(a);printf("%d\n",k);return 0;}

1024 Palindromic Number (25 分)大整数相加+会问+reverse倒置相关推荐

  1. 【题意分析】1024 Palindromic Number (25 分)_38行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  2. 【PAT甲级 BigInteger运算】1024 Palindromic Number (25 分) Java 全部AC

    题目 题解 Java 一开始用C++写的,有两个大数的测试点过不去,后来改用Java的BigInteger开挂,全部通过 import java.io.BufferedReader; import j ...

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

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

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

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

  5. 漫画:如何实现大整数相加

    转载自  漫画:如何实现大整数相加 在程序中列出的 "竖式" 究竟是什么样子呢?我们以 426709752318 + 95481253129 为例,来看看大整数相加的详细步骤: 第 ...

  6. 【Java】算法积累1——大整数相加

    算法刚刚入门,每次记录下自己所学的知识总结以及理解,希望能够帮助大家,如有内容有错误或可改进的地方,欢迎一起探讨,一起进步,在此也要感谢WYSCODER学长给我的学习建议 目录 方法一: 思路: 代码 ...

  7. 怎样实现大整数相加?

    问题: 如果给出两个很大很大的整数,这两个数大到long类型也装不下,比如100位整数,如何求它们的和呢? 分析: 回顾起小学数学,当我们需要计算两个较大数目的加减乘除,我们是用列竖式的方式来计算的. ...

  8. 1019 General Palindromic Number (20分)_18行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  9. 1024 Palindromic Number

    1. 本题给的N的范围是10位以内的整数,但是注意了不知要要和反序列相加多少次,因此大数的int d[]的大小10是远远不够,100才全部AC. 2. 一开始不通过不知道是位数不够,以为是到确定步数停 ...

  10. 华为面试题之大整数相加

    问题描述:在计算机中,由于处理器位宽限制,只能处理有限精度的十进制整数加减法,比如在32位宽处理器计算机中,参与运算的操作数和结果必须在-231~231-1之间.如果需要进行更大范围的十进制整数加法, ...

最新文章

  1. 8天学通MongoDB——第二天 细说增删查改
  2. flask的同一ip域名不同端口的两个网站session冲突
  3. node process.env
  4. java html api 百度云,Javase-6.0_中文API_HTML(最新更新)
  5. 代理模式(动态代理模式)
  6. 搭建servlet+jsp环境
  7. Qt-做一个快速打包插件(一键完成项目软件打包)
  8. 【sklearn第八讲】广义线性模型
  9. ASP.NET身份验证机制membership入门——API篇
  10. 7-4 谁会留下?规则如下:所有的学生绕成一圈,顺序排号,从第一个学生开始报数,凡是报到固定数字(例如 5)的都退出,直到只剩下一位学生游戏才中止。 (10 分)
  11. VS程序发布之NetFramework版本选择
  12. 声道测试音频_一音成佛的尺八音色,电吹管的单声道和双声道录音对比(2)
  13. (01)Structs初学笔记——开篇
  14. 【区块链 | Compound】4.剖析DeFi借贷产品之Compound:清算篇
  15. python绘制多边形的程序_Python – 绘制多边形
  16. 计算机公式求时间差公式,24时间差计算公式 excel中计算24小时
  17. 【软件安全】API HOOK
  18. MySQL Workbench生成数据表关系图
  19. Python标准库:一些受欢迎的模块
  20. Python抓取开源中国资讯(使用BeautifulSoup库)

热门文章

  1. 当你想对常用网站定制属于自己的颜色,然而又没人理你怎么办
  2. 替代top的系统监控工具glances
  3. c++primer 容器算法整理一
  4. IBM服务器渠道销售招聘,热招 | IT客户服务渠道销售,我们在IBM等你!
  5. Java基础,不需要复杂语句,使用for循环实现求出1~100之间的奇数和以及偶数和,超级简单
  6. Java的%=是什么意思?%=运算的正确使用
  7. c++ socket线程池_Netty(3)——Reactor线程模型
  8. css颜色跟背景总结(内含实例及截图)
  9. wpf 让图标显示在系统托盘
  10. Go语言编程(七牛云存储团队执笔)