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:
484 2Sample Input 2:
69 3Sample Output 2:
1353 3
http://www.patest.cn/contests/pat-a-practise/1024
注意点:
若输入的是
067 3
则输出为
7106
3
就是说前面的0是不能略去的,这个错误需要想到
ac1:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std ;
#define INF 999999
#define N 105
int n ;
int k ;
bool isPalindromic(string s)
{
int len = s.size();
int i , j ;
for(i = 0 ; i < len / 2 ; i++)
{
j = len - i - 1 ;
if(s[i] != s[j] )
return false ;
}
return true ;
}
string step(string s)
{
string rs = "" ;
int i , j ;
int len = s.size() ;
int carry = 0 ;
for(i = 0 ; i < len ; i++)
{
int c1 = s[i] - '0' ;
int c2 = s[len - i - 1] - '0';
int tmpc = c1 + c2 + carry ;
carry = tmpc / 10 ;
char nowc = tmpc % 10 + '0';
rs = nowc + rs ;
}
if(carry == 1)
rs = '1' + rs ;
return rs ;
}
int main()
{
//freopen("in.txt","r",stdin);
string s ;
int i ;
cin >> s >> k;
for(i = 0 ; i < k ;i++)
{
if( isPalindromic(s))
{
cout << s << endl ;
printf("%d\n" , i );
break ;
}
s = step(s);
}
if(i == k)
{
cout << s << endl ;
printf("%d\n" , i );
}
return 0 ;
}
ac2:
#include<stdio.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
#include <vector>
#include <string.h>
#include <string>
using namespace std;
vector<int> myadd(vector<int> v)
{
vector<int> vt ;
int len = v.size();
int i;
int carry = 0;
for (i = 0; i < len; i++)
{
int tmp = v.at(i) + v.at(len - 1 - i) + carry;
int nowc = tmp % 10 ;
carry = tmp / 10 ;
vt.push_back(nowc);
}
if (carry == 1)
vt.push_back(1);
return vt;
}
bool isOk(vector<int> v)
{
int len = v.size();
for (int i = 0; i < len / 2; i++)
{
if (v.at(i) != v.at(len - 1 - i))
return false;
}
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
int i;
char s1[20];
int k;
while( scanf("%s%d", s1, &k) != EOF)
{
int len = strlen(s1);
vector<int> v;
for (i = len-1; i >= 0; i--)
{
int tmp = (int)(s1[i] - '0');
v.push_back(tmp);
}
int num = 0;
while (num < k)
{
if (isOk(v))
break;
v = myadd(v);
num++;
}
int len2 = v.size();
for (i = len2 - 1; i >= 0; i--)
{
printf("%d", v.at(i));
}
printf("\n%d\n", num);
}
return 0;
}
ac3
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
const int INF = 0x7fffffff;
const int MIN_INF = - INF -1;
typedef long long int LL;
const int N = 10005;
string Add(string &s1, int len1, string &s2, int len2)
{
string ans ="";
int carry = 0;
int i = len1 -1;
int j = len2 -1;
while(i >= 0 && j >= 0)
{
int n1 = s1[i] - '0';
int n2 = s2[j] - '0';
int num = n1 + n2 + carry;
carry = num / 10;
char yushu = (num % 10) + '0';
ans += yushu;
i --;
j --;
}
if(i >= 0)
{
for(int k = i ; k >= 0; k--)
{
int n1 = s1[k] - '0';
int num = n1 + carry;
carry = num / 10;
char yushu = (num % 10) + '0';
ans += yushu;
}
}
if(j >= 0)
{
for(int k = j ; k >= 0; k--)
{
int n2 = s2[k] - '0';
int num = n2 + carry;
carry = num / 10;
char yushu = (num % 10) + '0';
ans += yushu;
}
}
if(carry != 0)
{
ans += (carry + '0');
}
string rvs(ans.rbegin(), ans.rend());
return rvs;
}
bool isPa(string &s, int len)
{
for(int i=0;i<len/2;i++)
{
if(s[i] != s[len - 1 - i])
return false;
}
return true;
}
int main()
{
//freopen("in.txt","r",stdin);
string s;
int k;
while(cin >> s >> k)
{
//cout << s << k << endl;
int cnt = 0;
for(int i=0;i<k;i++)
{
int len = s.size();
if(isPa(s,len))
{
break;
}
cnt ++;
string s2(s.rbegin(), s.rend());
int len2 = s2.size();
s = Add(s, len, s2,len2);
}
cout << s << endl;
cout << cnt << endl;
}
return 0;
}