On palindrome number

Time:2021-3-27

Title Description

For a natural number n, if the number N1 obtained by inversing the digits of n is equal to N, then n is called palindrome number, such as 2332.
If given an n (2 < = n < = 16) base number m (the length of M is less than 100 bits), if M is not palindrome number, it can be added in N base to get palindrome number.
For example, for the decimal number 79
STEP1 : 79 + 97 = 176
STEP2 : 176 + 671 = 847
STEP3 : 847 + 748 = 1595
STEP4 : 1595 +5951 = 7546
STEP5 : 7546 + 6457 = 14003
STEP6 : 14003 + 30041 = 44044
Then, for a given n-ary number m, please judge whether it can get palindrome number within 30 steps (including 30 steps).

Input format:
The first line contains a positive integer n (2 < = n < = 16).
The second line contains a positive integer m (within 100 bits).

Output format:
If the palindrome number can be obtained in N steps, output “Step = n”, otherwise output “no”.

Thinking of solving problems

1. Store high precision numbers in array (special judgment for hexadecimal numbers)
2. Reverse arrangement, judge whether it is palindrome number
3. If it is not palindrome number, high-precision n-ary addition is carried out
4. Cycle 2-3 steps until the conditions are met

Complete code

#include
#include
#define MAX_LEN 1000
char num[MAX_LEN + 10];
int sz1[MAX_LEN + 10];
int sz2[MAX_LEN + 10];

void reverse(int a[], int b[]) {
	int i;
	for (i = 1; i <= a[0]; i++) {
		b[i] = a[a[0] - i + 1];
	}
	b[0] = a[0];
}/*Function: reverse arrangement*/

int cmp(int a[], int b[]) {
	int i;
	for (i = 1; i <= a[0] / 2; i++) {
		if (a[i] != b[i]) {
			return 0;
		}
	}
	return 1;
}/*Function: judge palindrome number
   Return value: return 0 for non palindrome number, return 1 for palindrome number*/

void plus(int a[], int b[], int n) {
	int i;
	for (i = 1; i <= a[0]; i++) {
		a[i] += b[i];
		a[i + 1] += a[i] / n;
		a[i] %= n;
	}
	if (a[a[0] + 1] > 0) {
		a[0]++;
	}
}/*Function: high precision n-ary addition*/

int main()
{
	int N, i;
	scanf("%d", &N);
	scanf("%s", num);
	
	sz1[0] = strlen(num);
	for (i = 1; i <= sz1[0]; i++) {
		if (num[sz1[0] - i] >= 'A' && num[sz1[0] - i] <= 'F') {
			sz1[i] = num[sz1[0] - i] - 'A' + 10;
		}
		else if (num[sz1[0] - i] >= 'a' && num[sz1[0] - i] <= 'f') {
			sz1[i] = num[sz1[0] - i] - 'a' + 10;
		}/*Special judgment on hexadecimal number*/
		else {
			sz1[i] = num[sz1[0] - i] - '0';
		}
	}
	for (i = 0; i <= 30; i++) {
		reverse(sz1, sz2);
		if (cmp(sz1, sz2) == 1) {
			break;
		}
		plus(sz1, sz2, N);
	}
	
	if (i <= 30) {
		printf("STEP=%d", i);
	}
	else {
		printf("NO");
	}
	return 0;
}