1024 Palindromic Number

PAT 1024 Palindromic Number 【代码】PAT 1024 Palindromic Number。 阅读详情

1024 Palindromic Number (25 point(s))

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

 知识点:

1.大数加法(这里采用string完成,而不再转换到数组,更加减少了代码量);

需要再注意的是,读取到的数值高位反而在string的低位,因此需要先分别翻转两个加数(但是这里不需要)。同理,最后的结果和也需要翻转。

2.回文数判断(同样不用数组解决)。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool isPal(string s){
	for(int i=0;i<s.length()/2;i++){
		if(s[i]!=s[s.length()-1-i]) return false;
	}
	return true;
}
string add(string s){
	string res = ""; 
	int carry = 0;
	int len = s.length();
	for(int i=0;i<len;i++){
		int temp = (s[i]-'0')+(s[len-1-i]-'0')+carry;
		res += temp%10+'0';
		carry = temp/10; 
	}
	if(carry!=0) res+= carry+'0';
	reverse(res.begin(),res.end());//重要 
	return res;
}
int main(void){
	string N;
	int K;
	cin>>N>>K;
	bool flag = false;
	int i=0;
	while(i<K){
		if(isPal(N)){
			cout<<N<<endl<<i<<endl;
			flag=true;
			break;
		}
		N = add(N);
		i++;
	}
	if(!flag) cout<<N<<endl<<K<<endl;
	return 0;
}

 

Xilinx_RAM_IP核的使用 Xilinx_RAM_IP核的使用 说明:单口RAM、伪双口RAM、双口RAM的读写,以及RAM资源占用的分析。 阅读详情

相关推荐

2022年的1024了!| 关于1024,都有哪些有趣的事儿?——1024的那些故事!| 酷炫前端1024趣味粒子特效(附源码)| 附:搞怪鼠标点击,越点越大特效(附源码)

当提到1024,很多人会立刻想到计算机领域中的数字,其代表2的10次方,即2的幂。这一天,既然大家的节日,也是我的生日,1024,祝大家节日快乐!!!祝我生日快乐!

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 877

1024

打卡1204

weixin_44606952的博客 4120

嵌入式SPI总线抽象设计:跨平台驱动与分层解耦实践

SPI(串行外设接口)是嵌入式系统中应用最广泛的同步串行通信协议,其核心价值在于高吞吐、低开销与硬件简洁性。其工作原理基于主从架构、全双工/半双工时序及片选(CS)控制,依赖CPOL/CPHA组合定义四种标准模式。在工程实践中,SPI常面临硬件资源受限、多外设共用总线、MCU平台迁移等挑战,导致驱动复用率低、维护成本高。通过结构体+函数指针实现的分层抽象——将物理总线、协议适配与设备驱动解耦,可显著提升代码可移植性与扩展性。该方法天然支持硬件SPI与GPIO模拟SPI双模式切换,并已在STM32等主流MCU

weixin_42601134的博客 60

PAT 甲级 1024  Palindromic Number

1024 Palindromic Number (25 point(s)) 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 singl...

漫浸天空的雨色的备忘录~ 768

spark 适配达梦数据库

spark 适配达梦数据库 版本 spark3.0.0 scala2.12.10 DM Database Server 64 V8 步骤一: 引驱动包: SBT方式: "com.dameng" % "Dm8JdbcDriver18" % "8.1.0.157", maven方式: <dependency> <groupId>com.dameng</groupId> <artifactId>Dm8JdbcDriver18</artifac

_@Eva@_的博客 2091

手把手教你读懂蓝牙BQB射频测试报告:以PRBS9为例的实战分析

本文以蓝牙BQB射频测试中的PRBS9信号为例,提供了一份实战分析指南。文章深入解读了输出功率、调制特性、邻道泄漏等核心测试项,并分享了信号分析仪操作技巧、测试环境搭建要点及设备选型建议,旨在帮助工程师精准解读报告,高效定位问题,确保产品顺利通过认证。

weixin_29289821的博客 261

1024 Palindromic Number

1024 Palindromic Number解题代码测试结果问题整理 解题代码 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int nmax = 1000; char str[nmax]; int step, s; struct bign { int len; int p[nmax]; bign() { len = 0; memset(p

Aruasg的博客 188

使用Python下载ECMWF的ERA-5数据

一、注册账号 [官网]https://www.ecmwf.int/ 二、登录后获取下载key https://cds.climate.copernicus.eu/api-how-to 如图右侧红色框中的内容复制保存为TXT文件,并重命名为“.cdsapirc”,注意前面有点,保存在 C:\Users\username\.cdsapirc,(WINDOWS系统),username指当前电脑登录账户。 三、安装cdsapi库 以pycharm为例: 在pycharm中依次打开:File>setting

wise_sun的博客 7706

PAT甲级1024 Palindromic Number

PAT 甲级 1024 Palindromic Number 前言:此题用什么数据类型特别重要,如果用long的话会有两个测试点通不过(更别说int了,通不过的更多),此题要求输入的N<10的10次方,由于考虑到相加,可能就会超出long的范围,这就是这两个测试点不能通过的原因,所以要用String package higherlevel; import java.util.Scanner...

优快云_Lrcx的博客 398

PyTorch实战:A2C算法在Pendulum环境中的调参技巧与收敛优化

本文详细介绍了使用PyTorch实现A2C算法在Pendulum环境中的调参技巧与收敛优化方法。通过分析Pendulum环境特性、设计高效的网络架构、优化超参数设置以及提升训练稳定性,帮助开发者掌握强化学习中的policy-based方法。文章特别强调了奖励工程、优势估计改进和梯度裁剪等关键优化策略,为连续控制任务提供了实用解决方案。

rum55的博客 214

西门子S7-200 SMART 实战解析:滑动平均值滤波算法在模拟量信号处理中的应用

本文详细解析了在西门子S7-200 SMART PLC中实现滑动平均值滤波算法的实战应用。通过处理模拟量信号如温度、压力等,该算法有效抑制信号干扰,提升系统稳定性。文章涵盖算法原理、程序框架、信号采集及调试优化,为工业自动化工程师提供实用指南。

gitlab7runner的博客 1156

PAT甲级 1024 Palindromic Number

PAT甲级 1024 Palindromic Number 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 n...

旺崽的博客 2961

uniapp - 解决App端(安卓/苹果)使用uni.openlocation跳转第三方高德地图白屏问题,安卓苹果ios平台使用应用内置地图查看位置打不开,打包后App高德地图页面白屏不显示的问题!

uni-app,安卓Android,苹果ios,安卓app端,苹果App端,uniapp苹果安卓app,高德地图白屏,uniapp调用内置高德地图白屏,uniApp使用uni.openlocation空白不显示地图,uniappapp端如何解决openlocation地图不显示问题,打开第三方地图显示位置地图没反应,uni.openlocation跳转第三方高德地图,APP端白屏问题,使用uni.openlocation跳转到第三方导航软件的时候,app端跳转的是高德地图,但是白屏没有反应,打包发布后白

🏆 前端领域优质创作者 1万+

1024 Palindromic Number (25分)

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 n...

renly123的博客 211

Python爬虫selenium验证-中文识别点选+图片验证码案例

环境对模型进行训练,参考:https://github.com/sml2h3/dddd_trainer。能识别,但是发现默认识别率有点低,想要提升识别率,可以搭建。截图每个字符,并基于ddddocr识别。根据坐标,在验证码上进行点击。

dme.的博客 2165

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Day49: [PAT甲级] 1024 Palindromic Number (25分)

Day49: [PAT甲级] 1024 Palindromic Number (25分) 题源: 来自PAT真题库: https://pintia.cn/problem-sets/994805342720868352/problems/994805476473028608 代码: dirty code凑合看吧 #include<iostream> #include<string...

qq2215459786的博客 190

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

pta非零返回怎么解决_PTA 甲级 1024 Palindromic Number

藕粉海:PTA 甲级刷题记录​zhuanlan.zhihu.comstep0 题目step1 我的解法这是一道关于回文字符串处理的题。严格说起来没有太难的卡人点,就不总结什么经验了。我的第一版代码很快就写好,如下:#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string....

weixin_39635130的博客 1524

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 1025 PAT Ranking
下一篇: 1035 Password
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值