PAT(甲级)1024
来源:互联网 发布:自己有货 怎么上传美工 编辑:程序博客网 时间:2024/05/17 07:30
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 <iostream>#include <cstring>#include <cstdlib>using namespace std;bool Ispalindromic(char *str,char *p){int length = strlen(str);p[length] = '\0';int i=0;bool flag=true;for(i=0;i<length;i++){p[length-1-i] = str[i];if(str[i] != str[length-1-i]) flag =false;}return flag;}void Addstr(char *str,char *p){int length = strlen(str);int carry=0;int i;for(i=length-1;i>=0;i--){int num1=str[i]-'0';int num2=p[i]-'0';p[i] = '0'+(carry+num1+num2)%10;carry=(carry+num1+num2)/10;}if(carry){str[0]=carry+'0';str[1]='\0';}else str[0]='\0';strcat(str,p);}int main(){int K,count=0;char N[1000];char res[1000];cin >>N >>K;int k=K;while(k){if(Ispalindromic(N,res)){break;}else{if(count <=K){ count++; Addstr(N,res);//cout <<N <<endl;}else break;}k--;}cout <<N <<'\n' <<count <<endl;return 0;}
- PAT(甲级)1024
- 浙大pat甲级 1024
- PAT甲级1024
- PAT甲级 1024
- PAT 甲级 1024 Palindromic Number
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- 分享DCT,DST,Walsh,Hadamard,Haar和Slant图像处理程序
- 题目练习4
- HTML5 history新特性pushState、replaceState
- 预置Chrome浏览器默认主页和书签
- 沙盒机制、NSBundle、简单对象写入文件
- PAT(甲级)1024
- 白话经典算法系列之八 七大排序总结篇
- LeetCode 16_3Sum Closest
- 【正则表达式】——限制只能输入数字和小数点及位数
- git怎样删除未监视的文件untracked files
- android四大组件详解
- linux mysql 命令
- Android UI 设计之TitleBar
- PAT(甲级)1025