PAT甲级 1024. Palindromic Number (25)
来源:互联网 发布:mac钥匙串怎么关闭 编辑:程序博客网 时间:2024/06/01 08:28
题目:
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.
Sample Input 1:67 3Sample Output 1:
4842Sample Input 2:
69 3Sample Output 2:
13533思路:
数字反转,做加法时注意进位。
代码:
#include<iostream>#include<string>#include<vector>using namespace std;int main(){string s;vector<int> orig;vector<int> resv;vector<int> ans;int K;cin >> s >> K;int i,j,d;for (i = 0; i < s.size(); ++i){d = s[i] - '0';orig.push_back(d);}for (i = orig.size() - 1; i >= 0; --i){resv.push_back(orig[i]);}for (i = 0; i < K; ++i){if (orig == resv)break;else{int tmp = 0;int num;ans.clear();for (j = orig.size() - 1; j >= 0; --j){num = orig[j] + resv[j]+tmp;tmp = num / 10;num = num % 10;ans.push_back(num);}if (tmp != 0)ans.push_back(tmp);swap(resv, ans); //向量交换orig.clear();for (j = resv.size() - 1; j >= 0; --j){orig.push_back(resv[j]);}}}for (j = 0; j < orig.size(); ++j)cout << orig[j];cout << endl;cout << i << endl;system("pause");return 0;}
- 【PAT甲级】1024. Palindromic Number (25)
- 1024. Palindromic Number (25)PAT甲级
- PAT甲级练习1024. Palindromic Number (25)
- PAT甲级1024. Palindromic Number (25)
- PAT 甲级 1024. Palindromic Number (25)
- PAT甲级 1024. Palindromic Number (25)
- PAT 甲级 1024. Palindromic Number
- PAT甲级练习题A1024. Palindromic Number (25)
- PAT 甲级 1024 Palindromic Number
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- pat 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)-PAT甲级真题(大整数相加)
- PAT甲级.1019. General Palindromic Number (20)
- js常见的var that = this的用法
- 2017年的小总结年初至10月
- (Java)jfinal 实现微信现金红包发送,欢迎各种软件业务订制开发合作
- 使用dom4j读取xml配置文件
- 2万汉字编码大全
- PAT甲级 1024. Palindromic Number (25)
- Git 常用命令
- SpringBootApplication等价于 @Configuration @EnableAutoConfiguration @ComponentScan
- 如何将 eth0 和 wlan0 进行桥接 并且 wlan0 作为 AP 模式 供手机连接(手机静态获取ip)
- Nginx源码剖析--event类型模块
- 专有云到混合云,是云计算的下半场?
- C# wpf BitmapImage 跨线程访问
- ELK中filebeat组件的安装-04
- 关于汽车诊断OBD的理解(ISO15031-5)