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 ...
随机推荐
- spring cloud gateway - RequestRateLimiter
1. Official website 5.7 RequestRateLimiter GatewayFilter Factory The RequestRateLimiter GatewayFilte ...
- nowcoder 寻找(LCA)
这个题貌似是过的最少的? smeow一眼给出了一个单log的算法orz 首先求出x和y的lca, x和c的lca,y和c的lca, 然后分类讨论以下就行了 实际上只有三种情况 #include< ...
- mysql5.7.18.1修改用户密码报错ERROR 1054 (42S22): Unknown column 'password' in 'field list'解决办法
本意向修改一个用户的密码,网上搜到的命令为如下 mysql> update user set password=password(“新密码”) where user=”用户名”; 执行后报错 E ...
- Java锁----Lock实现原理
转载. https://blog.csdn.net/wl6965307/article/details/51249156 Lock完全用Java写成,在java这个层面是无关JVM实现的. 在java ...
- SpringBoot 之热部署
默认情况下, 我们修改 class 或者 修改模板文件(templates目录 下面的文件) 等动态资源, 都不会立即自动生效. 在IDEA中, 我通过Ctrl + F9 , 仍然是无效. 当然, 静 ...
- 活用RPM获取包的信息
rpm -q 功效大 如果你想要在系统上安装.卸载或是升级软件,需要对系统软件进行查询:或是有如下的场景: 安装了一个软件,需要知道这个软件的版本. 遇到一个文件,不认识它,需要知道它是什么软件,有什 ...
- Spring再接触 IOC DI
直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...
- Match-----Correlation-----find_ncc_model_exposure
* This example program shows how to use HALCON's correlation-based* matching. In particular it demon ...
- js计算base64文件流大小
getImgSize(str) { //获取base64图片大小,返回KB数字 var str = base64url.replace('data:image/jpeg;base64,', '');/ ...
- servlet3.1
Servlet3.1新增的新特性强制更改sessionId 由HttpServletRequest 的changeSessionId()方法实现 非阻塞式IO 非阻塞式IO我们应该知道Servlet底 ...