立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


题意:给定数N,次数K,将N翻转后与原数相加,判断是否为回文数,如果是则直接输出,如果不是,则算作一次操作,操作次数上限为K。若K次后仍不是回文数,则直接输出该数,结束循环。

分析:利用了字符串函数翻转机制缩减了代码量。代码逻辑很简单,读者可自行体会。


#include<bits/stdc++.h>
using namespace std;bool is_palindormic(string s) {     //判断是否是回文数 string s1 =s;reverse(s.begin(), s.end());if(s1 == s) return true;else return false;
}string ope (string s) {                //执行相加操作 string s1 = s, add_s;reverse(s.begin(), s.end());int carry = 0, x;for(int i = 0; i < s.size(); i++) {x = (s[i]-'0' + s1[i]-'0' + carry) % 10;carry = (s[i]-'0' + s1[i]-'0' + carry) / 10;add_s += x+'0';}if(carry > 0) add_s += carry+'0';reverse(add_s.begin(), add_s.end());return add_s;
}int main() {string s; int n, n1;cin >> s >> n;n1 = n;while(n1) if(is_palindormic(s)) {    //如果是回文数,则输出,结束循环 cout << s << '\n' << n-n1 << '\n'; goto loop;} else {               //如果不是回文数, n1--;s = ope(s);}cout << s << '\n' << n << '\n'; loop : ;return 0;
}

耗时:

【题意分析】1024 Palindromic Number (25 分)_38行代码AC相关推荐

  1. 1019 General Palindromic Number (20分)_18行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  2. 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...

  3. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  4. 【附超时原因】1055 The World‘s Richest (25 分)_42行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Forbes magazine publishes every year its list of billionaires bas ...

  5. 【简洁代码】1028 List Sorting (25 分)_26行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Excel can sort records according to any column. Now you are suppo ...

  6. 【PAT甲级 BigInteger运算】1024 Palindromic Number (25 分) Java 全部AC

    题目 题解 Java 一开始用C++写的,有两个大数的测试点过不去,后来改用Java的BigInteger开挂,全部通过 import java.io.BufferedReader; import j ...

  7. 【题意+分析】1071 Speech Patterns (25 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 People often have a preference among synonyms of the same word. F ...

  8. 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...

  9. 1039 Course List for Student (25 分)_33行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Zhejiang University has 40000 students and provides 2500 courses. ...

最新文章

  1. java web调用c_Java调用C/C++程序
  2. 一步一步学Silverlight 2系列(12):数据与通信之WebClient
  3. 自动感光调节LED灯亮度C语言程序,单片机PWM调节LED灯亮度
  4. CodeForces 1616H Keep XOR Low {a^b≤x} / CodeForces gym102331 Bitwise Xor {a^b≥x}(trie树 + 计数)
  5. 加速业务交付,从 GKE 上使用 Kubernetes 和 Istio 开始
  6. java固定数组_Java在固定长度的数组里加入一个数
  7. 分布式任务队列--Celery的学习笔记
  8. git push --set-upstream
  9. 2021年认证杯SPSSPRO杯数学建模D题(第一阶段)停车的策略全过程文档及程序
  10. C++题解:矩阵快速幂 求 斐波那契数列
  11. 素材网站整理(持续更新ing~)
  12. java 对战平台-魔兽版
  13. 2022爱分析・数据库厂商全景报告 | 爱分析报告
  14. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(一)服务器端
  15. python return返回值格式化_day11__函数名的应用,python新特f-strings格式化输出、迭代器...
  16. [Tensorflow2] 梯度反转层(GRL)与域对抗训练神经网络(DANN)的实现
  17. 接口开发任务完成中(待续)
  18. js 动态加版块进页面
  19. Spring简化Java开发_java开发如何用spring简化?详细图解
  20. 如何带领团队?各位指点江山吧!

热门文章

  1. 分布式系统概念 | 分布式ID:数据库、号段模式、雪花算法(Snowflake)、Redis实现方案
  2. Linux下查看系统配置
  3. Python selenium对js元素进行增删改查操作
  4. spring源码构建以及模块划分和依赖
  5. GitHub被中国人霸榜!国外开发者不开心了
  6. OS- -计算机硬件简介
  7. LiveVideoStackCon 2022 上海站延期通告(内附最新日程海报)
  8. 【线上分享】机器视觉编码标准与技术进展
  9. 聚焦新基建,腾讯云十余项自研技术应用集中亮相
  10. 万字详解:腾讯如何自研大规模知识图谱 Topbase