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.

# string int 互换法

  • stod()
  • to_string()
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = ; bool isPalindromic(string v){
int l=, r=v.length()-;
while(l<r){
if(v[l]!=v[r]) return false;
l++; r--;
}
return true;
} string Add(string n,string rn){
long a=stod(n);
long b=stod(rn);
return to_string(a+b);
} int main(){
string n;
int k;
cin>>n;
scanf("%d",&k);
if(isPalindromic(n)){
cout<<n<<endl;
printf("");
return ;
}
int t=;
while(t<k){
t++;
string rn=n;
reverse(rn.begin(), rn.end());
n=Add(n,rn);
if(isPalindromic(n)){
cout<<n<<endl;
printf("%d",t);
return ;
}
}
cout<<n<<endl;
printf("%d",t);
}

# 大整数相加法

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = ; bool isPalindromic(string v){
int l=, r=v.length()-;
while(l<r){
if(v[l]!=v[r]) return false;
l++; r--;
}
return true;
} string Add(string n,string rn){
string ans="";
int i,jinwei=;
for(i=;i<n.length();i++){
ans += ((n[i]-'')+(rn[i]-'')+jinwei)% + '';
jinwei = ((n[i]-'')+(rn[i]-'')+jinwei)/ ;
}
if(jinwei) ans += jinwei+'';
reverse(ans.begin(), ans.end());
return ans;
} int main(){
string n;
int k;
cin>>n;
scanf("%d",&k);
if(isPalindromic(n)){
cout<<n<<endl;
printf("");
return ;
}
int t=;
while(t<k){
t++;
string rn=n;
reverse(rn.begin(), rn.end());
n=Add(n,rn);
if(isPalindromic(n)){
cout<<n<<endl;
printf("%d",t);
return ;
}
}
cout<<n<<endl;
printf("%d",t);
}

1024 Palindromic Number int_string转换 大整数相加的更多相关文章

  1. PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

    题目 A number that will be the same when it is written forwards or backwards is known as a Palindromic ...

  2. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  3. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  4. poj2305-Basic remains(进制转换 + 大整数取模)

    进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串-&g ...

  5. 华为"128为大整数相加"机试题

    最近正直春招,偶尔接触到了华为的这道大整数相加的测试题,在网上找了一个算法,然后自己尝试进行了优化,最后也对memmove()函数效率有了进一步把握. #include <time.h># ...

  6. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  7. SOJ 1002/1003/1004 大整数相加/相乘/相除

    三个题目分别考察大整数相加相乘相除运算.如果按照传统算法是取一个长数组,之后进行模拟或者FFT来进行运算.但是相对繁琐. 后来昨天的青岛区域赛网赛1001,用到了JAVA的BigDecimal,于是反 ...

  8. 1024 Palindromic Number

    题意: 给出一个数N(N<=10^10),最多可操作K次(K<=100),每次操作为这个数和其反转之后的数相加,若得到的结果为回文数,则输出:若在K次迭代后仍然不是回文数,在输出第K次操作 ...

  9. PTA (Advanced Level) 1024 Palindromic Number

    Palindromic Number A number that will be the same when it is written forwards or backwards is known ...

随机推荐

  1. java效率取随机不重复数

    //效率取随机不重复数 public int[] takeRandom(int num) { Random rd = new Random(); int[] rds = new int[num];// ...

  2. mysql 表映射为java bean 手动生成。

    在日常工作中,一般是先建表.后建类.当然也有先UML构建类与类的层级关系,直接生成表.(建模)这里只针对先有表后有类的情况.不采用代码生成器的情况. 例如: 原表结构: ),)) BEGIN ); ) ...

  3. 面试回顾——List<T>排序

    1.如何对List<T>排序: public static void main(String[] args) { Student stu1=new Student("张三&quo ...

  4. jmeter 的安装与配置

    环境配置: 操作系统:win10 JDK:1.8 jmeter:5.0 jmeter 是 java 程序.所以要运行 jmeter 需要先安装配置 jdk. 1.安装配置 jdk 官方网站下载 jdk ...

  5. R语言-线图(二)

      1.线图示例 plot()为高水平作图命令,axis().lines().legend()都为低水平作图命令 > rain<-read.csv("cityrain.csv&q ...

  6. P45 实践作业

    1. 影评: 观众数量多少,决定被虐者死亡速度的快慢.这一新奇但是残忍的想法,无疑是<网络杀机>的点睛之笔.公众.媒体对凶手网站主造成的伤害,比起那些用恶毒言论还要让人难受千百倍.他是一个 ...

  7. php 获取数组深度的key

    1.数组 深度遍历 function fun($a,&$b) { foreach ($a as $k=>$val) { if (is_array($val)) { $b[]=$k; fu ...

  8. 《CSAPP》符号解析

    符号解析 链接器解析符号引用的方法是将每个引用与它输入的可重定位目标文件的符号表中的一个确定的符号定义联系起来.编译器只允许每个模块中每个本地符号只有一个定义. 对于全局符号,当编译器遇到一个不是在当 ...

  9. 最小的K个数(python)

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,.   # -*- coding:utf-8 -*- class So ...

  10. POJ-1797.HeavyTransportation(最长路中的最小权值)

    本题思路:最短路变形,改变松弛方式即可,dist存的是源结点到当前结点的最长路的最小权值. 参考代码: #include <cstdio> #include <cstring> ...