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. mysql中间件kingshard

    这样写是OK的: select * from bind_history limit 10;select id, passport_id, person_id, create_time, cast(is ...

  2. 统计uint64的数对应二进制数的1的个数

    // pc[i] is the populatio  count of ivar pc [256]byte //统计出o~255每个数对应二进制上1的个数func init() {    for i ...

  3. 204. jetcache:在Spring Boot中怎么玩?

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

  4. Spring线程池的5个要素

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

  5. IntelliJ IDEA 调试技巧

    程序员的工作内容,有不少的时间是用在调试代码上.可以说不是在调试代码,就是即将调试代码. 掌握调试代码的一些技巧,在使用IDE提供的debugger时会快速定位问题的方式. 1.多线程调试 在多线程应 ...

  6. 30_react_router基本使用

    项目结构: import React from 'react' import {render} from 'react-dom' import {BrowserRouter} from 'react- ...

  7. POI richText和html的转换案例

    private XSSFRichTextString parseHtmlStrToRichText(String htmlStr) { Document document = parseHtmlStr ...

  8. SSO 单点登录

    一.单系统登录机制 1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务器会独立处理,不与之前或之后的请求产生关 ...

  9. MySQL中的重做日志(redo log),回滚日志(undo log),以及二进制日志(binlog)的简单总结

    MySQL中有六种日志文件,分别是:重做日志(redo log).回滚日志(undo log).二进制日志(binlog).错误日志(errorlog).慢查询日志(slow query log).一 ...

  10. 25.Hibernate-配置文件.md

    目录 1.主配置文件 1.1定义 1.1.1分类 1.1.2分类 1.1.3不使用配置文件生成表 1.2教程 2. 映射配置文件 1.主配置文件 1.1定义 1.1.1分类 在hibernate的配置 ...