[NOIP1999 Popularization Group] Palindrome Number
Title description:
If a number (with a non-zero first digit) reads the same from left to right as from right to left, we call it a palindromic number.
For example: Given a decimal number 56, add 56 to 65 (that is, read 56 from right to left), and get 121$ which is a palindrome.
Another example: for the decimal number 87:
STEP1:87+78=165
STEP2:165+561=726
STEP3:726+627=1353
STEP4:1353+3531=4884
One step here refers to an N$ addition, and the above example used at least 4 steps to get the palindrome number 4884.
Write a program, given an N (2<=10 or N=16) base number M (within 100$ digits), find the minimum number of steps to get the palindrome number. If it is impossible to get the palindrome within 30$ steps (including 30 steps), then output `Impossible!`.
Input format:
Two lines, respectively N, M.
Output format:
If the palindrome can be obtained within 30$ steps, the output format is `STEP=ans`, where ans is the minimum number of steps to obtain the palindrome.
Otherwise output `Impossible!`.
Example :
Sample input #1
10
87
Sample output #1
STEP=4
Ideas:
This question is a question about the use of a simple algorithm, which is equivalent to finding a high-precision palindrome.
because:
Since it is an N-ary number.
Please change --%10 in high precision processing to %n.
Please change --/10 in high precision plus to /n.
Other calculation methods remain unchanged.
First define the variable:
int n, q[1000001], l, w[1000001], ans;
string s;
q is a high-precision array, w is the reversed array of q, l is the length of the high-precision number, n is the base, ans is the number of steps required, and s is the input high-precision string.
High-precision plus code:
void add(int a[], int b[])//高精加
{
for(int i = 1; i <= l; i++)
{
a[i] += b[i];
a[i + 1] += a[i] / n;//进位
a[i] %= n;
}
if(a[l + 1] > 0)//考虑从最高位进位到最高位的下一位
{
l++;//长度++
}
}
High-precision reverse code:
void turn(int a[])//反转数字
{
int j = 0;
for(int i = l; i >= 1; i--)//反着存
{
w[++j] = a[i];//存到w数组里
}
}
High-precision judgment palindrome code:
bool f(int a[])//判断是否是回文数
{
int ln = l;
int i = 1;//从两边判断
int j = l;
while(ln--)
{
if(ln < l / 2)//判一般就可以啦QAQ
{
break;
}
if(a[i] != a[j])
{
return false;//有一位不相等就不是回文数
}
i++;
j--;
}
return true;
}
Array storage high-precision code:
void init()//把s字符串附到q数组里
{
int j = 0;
for(int i = s.length() - 1; i >= 0 ; i--)
{
if(s[i] >= '0' && s[i] <= '9')//数字
{
q[++j] = s[i] - '0';
}
else//还有十六进制的
{
q[++j] = s[i] - 'A' + 10;
}
}
}
main main function code:
int main()
{
cin>>n>>s;
init();//初始化数组
l = s.length();
while(!f(q))//是否回文
{
turn(q);
add(q, w);//加上回文数
ans++;
if(ans > 30)//步数大于三十就退出
{
break;
}
}
if(ans > 30)
{
printf("Impossible!"); //叹号注意
}
else
{
printf("STEP=%d", ans);
}
return 0;
}
Full code:
#include <bits/stdc++.h>
using namespace std;
int n, q[1000001], l, w[1000001], ans;
string s;
void init()
{
int j = 0;
for(int i = s.length() - 1; i >= 0 ; i--)
{
if(s[i] >= '0' && s[i] <= '9')
{
q[++j] = s[i] - '0';
}
else
{
q[++j] = s[i] - 'A' + 10;
}
}
}
void add(int a[], int b[])
{
for(int i = 1; i <= l; i++)
{
a[i] += b[i];
a[i + 1] += a[i] / n;
a[i] %= n;
}
if(a[l + 1] > 0)
{
l++;
}
}
bool f(int a[])
{
int ln = l;
int i = 1;
int j = l;
while(ln--)
{
if(ln < l / 2)
{
break;
}
if(a[i] != a[j])
{
return false;
}
i++;
j--;
}
return true;
}
void turn(int a[])
{
int j = 0;
for(int i = l; i >= 1; i--)
{
w[++j] = a[i];
}
}
int main()
{
cin>>n>>s;
init();
l = s.length();
while(!f(q))
{
turn(q);
add(q, w);
ans++;
if(ans > 30)
{
break;
}
}
if(ans > 30)
{
printf("Impossible!");
}
else
{
printf("STEP=%d", ans);
}
return 0;
}
Summarize:
This question requires many basic algorithms such as high-precision addition, high-precision inversion, high-precision judgment of palindrome numbers, and high-precision array storage.
Topic link:
[NOIP1999 Popularization Group] Palindrome Number - Luogu https://www.luogu.com.cn/problem/P1015