1079 delayed palindromes (20 points)
Given a k+1 Bit positive integer N. Write ak⋯a1a0 The form in which all i yes 0≤ai<10 And ak>0. N It is called a palindrome number if and only if for all i yes ai=ak−i. Zero is also defined as a palindrome number.
Non palindromes can also be changed into palindromes through a series of operations. First, reverse the number, and then add the reverse number to the number. If the sum is not a palindrome number, repeat the reverse and add operation until a palindrome number appears. If a non palindrome number can change into a palindrome number, it is called the delayed palindrome number. (definition translated from) https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, this question requires you to find the palindrome number.
Input format:
Input gives a positive integer with no more than 1000 bits in one line.
Output format:
The process of outputting the number of palindromes for a given integer line by line. The format of each line is as follows
A + B = C
among A It's the original number, B yes A Number of reversals, C It's their sum. A Start with the integer entered. Repeat until C It becomes the palindrome number within 10 steps. At this time, it is output in one line C is a palindromic number.; Or if you can't get the palindrome number in 10 steps, output it in one line at last Not found in 10 iterations..
Input example 1:
97152
No blank lines at the end
Output example 1:
97152 + 25179 = 122331 122331 + 133221 = 255552 255552 is a palindromic number.
No blank lines at the end
Input example 2:
196
No blank lines at the end
Output example 2:
196 + 691 = 887 887 + 788 = 1675 1675 + 5761 = 7436 7436 + 6347 = 13783 13783 + 38731 = 52514 52514 + 41525 = 94039 94039 + 93049 = 187088 187088 + 880781 = 1067869 1067869 + 9687601 = 10755470 10755470 + 07455701 = 18211171 Not found in 10 iterations.
No blank lines at the end
This problem is similar to the digital black hole. The trouble lies in the 1000 digit number. We can only use the string to solve it, but if the sum of each bit of the string is > = 10, it needs to carry. And there will be a problem when strlen has 0 in the string array!
So I think of a way to make the character with the result of 0 equal to - 2.
I. first, reverse A. I wanted to use strrev, but I can't use c.
int zhuanhuan(char* A, int n, char* B) { int j = 0; for (int i = strlen(A) - 1; i >= 0; i--) { B[j++] = A[i]; //Transfer the elements of A into B } }
2, Enter the huiwen function, and operate as follows:
int huiwen(char* A, int n, int t) { int j = 0; char B[1001] = { 0 }, C[1001] = { 0 }; zhuanhuan(A, n, B); //Call the zhuanhuan function to obtain the inversion of A and store it in B if (t >= 10) { printf("Not found in 10 iterations."); return 0; } if (strcmp(A, B) == 0) Compare before inversion A And after reversal B Whether it's the same, not the same, continue to execute, the same printf Then jump out. { printf("%s is a palindromic number.", A); return 0; } for (int i = 0; i < strlen(A); i++) //For each bit, the sum of A and B is stored in C. if there is 0 + 0, let 0 = - 2; Because if there is 0 for strlen (c), it will be calculated to 0, and the following numbers will not be calculated into the length. { C[j] += B[i] - '0' + A[i] - '0'; if (C[j] == 0) { C[j] = -2; } if(C[j]>=10) { C[j] -= 10; if (C[j] == 0) { C[j] = -2; } C[j + 1] += 1; } j++; } for (int k = 0; k < strlen(C); k++) { if (C[k] < 0) { C[k] = C[k] + 2 + '0'; //Change all elements in C + '0' into characters and numbers. } else { C[k] += '0'; } } if (t < 1) { printf("%s + %s = ", A, B); } else { printf("%s + %s = ", B, A); }t++; //t is used to count. When it reaches ten times and does not meet the conditions, it prints F, and then ends the program. for (int l = strlen(C) - 1; l >= 0; l--) { printf("%c", C[l]); } printf("\n"); return huiwen(C, strlen(C), t); }
Finally, attach the whole code:
#include<stdio.h> #include<string.h> int zhuanhuan(char* A, int n, char* B) { int j = 0; for (int i = strlen(A) - 1; i >= 0; i--) { B[j++] = A[i]; } } int huiwen(char* A, int n, int t) { int j = 0; char B[1001] = { 0 }, C[1001] = { 0 }; zhuanhuan(A, n, B); if (t >= 10) { printf("Not found in 10 iterations."); return 0; } if (strcmp(A, B) == 0) { printf("%s is a palindromic number.", A); return 0; } for (int i = 0; i < strlen(A); i++) { C[j] += B[i] - '0' + A[i] - '0'; if (C[j] == 0) { C[j] = -2; } if(C[j]>=10) { C[j] -= 10; if (C[j] == 0) { C[j] = -2; } C[j + 1] += 1; } j++; } for (int k = 0; k < strlen(C); k++) { if (C[k] < 0) { C[k] = C[k] + 2 + '0'; } else { C[k] += '0'; } } if (t < 1) { printf("%s + %s = ", A, B); } else { printf("%s + %s = ", B, A); }t++; for (int l = strlen(C) - 1; l >= 0; l--) { printf("%c", C[l]); } printf("\n"); return huiwen(C, strlen(C), t); } int main() { char N[1001]; int t = 0; scanf("%s", N); huiwen(N, strlen(N), t); }