浙大 PAT Advanced level 1024. Palindromic Number (25)
来源:互联网 发布:淘宝实名制可以改吗 编辑:程序博客网 时间:2024/05/22 00:45
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 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
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 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3
初始数字<=10^10,操作次数<=100,假定逆修相加的结果约等于原数字的两倍,则处理过程中可能出现的最大数字范围(10^10 )*(2^100),远远超过int的范围,甚至也超过了long long int
的范围,因此要考虑用string或者数组来处理数据。
特别需要说明的一点,case中可能有类似“0001 3”的数据,在处理输入的时候要特别考虑前面的0.
#if 1#include <iostream>#include <vector>#include <deque>#include <string>using namespace std;deque<int> number;deque<int> reverse;int N, K;void push_into_deque(long long int target){while (target != 0){number.push_front(target%10);target /= 10;}return ;}bool isPalindromic(){for (int i = 0; i != number.size()/2; ++i){if (number[i] != number[number.size()-i-1]){return false;}}return true;}void compute_reverse(){reverse.resize(number.size());for (int i = 0; i != number.size(); ++i){reverse.at(i) = number.at(number.size()-1-i);}return ;}void compute_sum(){int tempsum = 0;int carry = 0;for (int i = number.size()-1; i != -1; --i){tempsum = number[i] + reverse[i] + carry;carry = tempsum/10;tempsum %= 10;number[i] = tempsum;}if (0 != carry){number.push_front(carry);}return ;}void print_number(){for (int i = 0; i != number.size(); ++i){cout << number.at(i);}cout << endl;}int main(){string str;cin >> str >> K;number.resize(str.size());for (int i = 0; i != number.size(); ++i){number[i] = str[i] - '0';}/*如果将上面的代码替换为如下, 则不能通过最后两个测试点, 怀疑有类似"0001 3"之类的测试数据cin >> N >> K;push_into_deque(N);*/for (int i = 0; i != K+1; ++i){if (isPalindromic() || i == K){print_number();cout << i << endl;break;}compute_reverse();compute_sum();}system("pause");return 0;}#else/* long long int 存储结果, 输入 999999999 100,, 计算22次便会溢出, case6、8通不过, 18分*/#include <iostream>#include <vector>using namespace std;long long int N, reverse;int K;vector<int> nvect;void push_into_vector(long long int target){while (target != 0){nvect.push_back(target%10);target /= 10;}}bool isPalindromic(){for (int i = 0; i != nvect.size()/2; ++i){if (nvect[i] != nvect[nvect.size()-i-1]){return false;}}return true;}long long int compute_reverse(){long long int ret = 0;for (int i = 0; i != nvect.size(); ++i){ret = ret*10 + nvect[i];}nvect.clear();return ret;}int main(){cin >> N >> K;for (int i = 0; i != K+1; ++i){push_into_vector(N);if (isPalindromic() || i == K){cout << N << endl;cout << i << endl;break;}reverse = compute_reverse();N += reverse;}system("pause");return 0;}#endif
0 0
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- 【PAT Advanced Level】1024. Palindromic Number (25)
- 【PAT】【Advanced Level】1024. Palindromic Number (25)
- 浙大 PAT Advanced level 1019. General Palindromic Number (20)
- 1024. Palindromic Number (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) 1024. Palindromic Number (25) 字符串反转相加
- 浙大PAT 1024. Palindromic Number (25)
- PAT (Advanced Level) Practise 1024 Palindromic Number (25)
- PAT (Advanced Level) Practise 1024 Palindromic Number (25)
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- PAT (Advanced) 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)【简易大数加法+回文】——PAT (Advanced Level) Practise
- 浙大 PAT Advanced level 1029. Median (25)
- 浙大PAT 1024题 1024. Palindromic Number
- 1019. General Palindromic Number (20) @ PAT (Advanced Level) Practise
- 【PAT Advanced Level】1019. General Palindromic Number (20)
- 【c++】PAT (Advanced Level)1019. General Palindromic Number (20)
- Pat(Advanced Level)Practice--1019(General Palindromic Number)
- java 后台开发中model与entity(实体类)的区别
- 【Android学习札记】Android Studio 安装配置
- win下cygwin openwrt编译测试 (应该行得通,但是我找不出来working g++ failed原因。)
- Java已经开始影响这一代程序员
- 模拟自选股(持续更新)
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- Android 开发之SQLITE 数据库的使用
- laravel php artisan 错误
- 查看开源项目org.gradle.api.internal.project.ProjectInternal.getPluginManager()Lorg/gradle/api/internal/plu
- 高质量C编程00-汇总
- hdu1026Ignatius and the Princess I(BFS + 优先队列)
- java 图片压缩和分辨率处理
- 基于ReentrantLock封装的字符串锁的场景与实现
- BZOJ 2160: 拉拉队排练