PAT(甲级)1024
2015-09-24 19:40
405 查看
1024. Palindromic Number (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:
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;
}
相关文章推荐
- 沙盒机制、NSBundle、简单对象写入文件
- POJ 2828 Buy Tickets
- 正在腾飞的教育界,你了解多少?
- 2016 百度研发岗面试总结
- PAT(甲级)1023
- mysql中You can't specify target table for update in FROM clause错误 转
- PAT(甲级)1022
- Struts2的action配置方法
- Cocos2d-x中的CC_CALLBACK_0,1,2,3
- 第二次作业(个人项目实践)
- 1 kvo(键值监听)
- Maven的配置文件pom.xml
- iOS_XML与JSON解析
- Android中自定义弹窗提醒控件
- 策略模式
- Linux chmod 命令
- 新电脑安装系统时提示 File:\Boot\BCD Status:0xc000000e 错误解决方案
- 今天终于申请到了CSDN,我来了
- poj-3065
- Android学习实践:7.使用布局文件进行事件绑定