1024 Palindromic Number int_string转换 大整数相加
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 (≤1010) 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转换 大整数相加的更多相关文章
- 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 ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- poj2305-Basic remains(进制转换 + 大整数取模)
进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串-&g ...
- 华为"128为大整数相加"机试题
最近正直春招,偶尔接触到了华为的这道大整数相加的测试题,在网上找了一个算法,然后自己尝试进行了优化,最后也对memmove()函数效率有了进一步把握. #include <time.h># ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- SOJ 1002/1003/1004 大整数相加/相乘/相除
三个题目分别考察大整数相加相乘相除运算.如果按照传统算法是取一个长数组,之后进行模拟或者FFT来进行运算.但是相对繁琐. 后来昨天的青岛区域赛网赛1001,用到了JAVA的BigDecimal,于是反 ...
- 1024 Palindromic Number
题意: 给出一个数N(N<=10^10),最多可操作K次(K<=100),每次操作为这个数和其反转之后的数相加,若得到的结果为回文数,则输出:若在K次迭代后仍然不是回文数,在输出第K次操作 ...
- PTA (Advanced Level) 1024 Palindromic Number
Palindromic Number A number that will be the same when it is written forwards or backwards is known ...
随机推荐
- Emscripten 安装和使用
OS: Windows 10 x64 I. install 0. pre install Python2.7 Node js Java 1. down git clone https://github ...
- 消息对话框 MessageBoxButtons
消息对话框MessageBox经常用于向用户显示通知信息.例如,在操作过程中遇到错误或程序异常,经常会使用这种方式给用户以提示,它是特殊类型的对话框. 在C#中,MessageBox消息对话框位于Sy ...
- wepy打开页面首次不显示,但是数据已经有了
page页面首次打开异步数据无法通过props传递到子组件 解决:在开发者工具关闭上传代码时自动压缩就解决了,在wepy文档里也有强调
- java 解耦
探索目的:使用的时候,只需要一次调用就可以完成日常复杂的操作.而工具类的高耦合带来业务代码维护复杂度加大.以 java 的思想对原始工具加以改进. 有没有比这里尝试的方法更高明的,在 A_Utils. ...
- js 模拟css3 动画1
<html> <head> <title> javaScript缓动入门 </title> </head> <body> < ...
- (转).net反编译工具JustDecompile
开源的反编译工具 JustDecompile https://www.telerik.com/blogs/justdecompile-engine-becomes-open-source https ...
- Lua中面向对象
一.Lua中类的简单实现: (1)版本——摘自 Cocos2.0中的: --Create an class. function class(classname, super) local superT ...
- WM-结汇
PI发给客户以后, 客户打过来定金之后. 把PI的 P单词去掉,改为只剩invoice, 再把PI改成一份contract为标题的文件,两份文件都是要自己和客户的签字的. 然后需要用这两份文件进行结汇 ...
- webpack浅析---出口篇
webpack有四个核心概念: 入口(entry) 输出(output) loader 插件(plugins) 输出: 在哪里输出创建的bundles,以及如何命名这些文件, 默认./dist fil ...
- node.js中path路径模块的使用
path模块是node.js中处理路径的核心模块.可以很方便的处理关于文件路径的问题. join() 将多个参数值合并成一个路径 const path = require('path'); conso ...