PAT A1024. Palindromic Number (25)
来源:互联网 发布:网络链接不上 编辑:程序博客网 时间:2024/06/05 03:07
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 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 <cstdio>#include <cstring>#include <vector>using namespace std;char s[20];int k, step = 0;struct Bignum{int d[1000], len;Bignum(){memset(d, 0, sizeof(d));len = 0;}};Bignum change(char s[]){Bignum c;c.len = strlen(s);for(int i = 0; i < c.len; i++){c.d[i] = s[c.len-1-i] - '0';}return c;}bool judge(Bignum c){bool ans = true;for(int i = 0; i < c.len; i++){if(c.d[i] != c.d[c.len-1-i]){ans = false;break;}}return ans;}Bignum add(Bignum a, Bignum b){Bignum c;int carry = 0, temp;for(int i = 0; i < a.len || i < b.len; i++){temp = a.d[i] + b.d[i] + carry;c.d[c.len++] = temp % 10;carry = temp / 10;}if(carry) c.d[c.len++] = carry;return c;}Bignum compute(Bignum a){Bignum b;b.len = a.len;for(int i = 0; i < b.len; i++) b.d[i] = a.d[a.len-1-i];return add(a, b);}int main(){scanf("%s %d", s, &k);Bignum p = change(s);while(!judge(p) && step < k){p = compute(p);step++;}for(int i = p.len-1; i >= 0; i--) printf("%d", p.d[i]);printf("\n%d", step);return 0;}
- PAT-A1024. Palindromic Number (25)
- pat-a1024. Palindromic Number (25
- PAT A1024. Palindromic Number (25)
- PAT甲级练习题A1024. Palindromic Number (25)
- A1024. Palindromic Number (25)
- 大数运算-PAT A1024.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)
- PAT-A1024
- 【PAT Advanced Level】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)【精华】
- 浙大PAT 1024. Palindromic Number (25)
- PAT (Advanced) 1024. Palindromic Number (25)
- 来来来,看看Bitcoin创世区块长什么样
- mysql悲观锁总结和实践
- Android引入依赖
- 数字从小到大排序中间加"*"打印成三角形以及倒置
- python 字典clear()方法
- PAT A1024. Palindromic Number (25)
- multi-class logestic regression的python实现
- 看完这,再也不用为项目报告发愁了
- 基于redis的setnx()、get()、getset()方法 分布式锁
- 3分钟通过一个App的演示深入理解区块链运行原理
- GCD(二)
- Netty5的客户端
- Nmap扫描漏洞
- angularJS简介和几个重要的例子