Problem description if one number (first not 0) reads from left to right and from right to left, we call it a palindrome number.
For example: Given a 10 binary number 56, will 56 plus 65 (that is, 56 right-to-left reading), to get 121 is a palindrome number.
Another example: for 10 binary number 87:
step1:87+78 = 165 step2:165+561 = 726
step3:726+627 = 1353 step4:1353+3531 = 4884
One step here is to make an n-ary addition, and the above example uses at least 4 steps to get the palindrome number 4884.
Write a program, given an N (2<=n<=10 or n=16) binary number m (where 16 binary number is 0-9 and a-f), the minimum number of steps can be obtained palindrome numbers.
If the palindrome number cannot be obtained within 30 steps (including 30 steps), the output "impossible!" Input format two lines, N and M output format if you can get palindrome number within 30 steps, Output "step=xx" (without quotation marks), where xx is the number of steps; otherwise the output line "impossible!" (without quotation marks) sample input 9
87 Sample Output Step=6Analysis:
divided N = 16 and 2<=n<=10 Two case discussion, Str receives the given string
Set in 30 cycles to determine whether STR is a palindrome string, not the inverse of STR and str Add, the result is first assigned to STR1,
And then back to Str.
#include <stdio.h>#include<string.h>#include<stdlib.h>#defineM 30CharStr[m],str1[m];intLen;intispalindrome ()//Judging palindrome number {inti; for(i=0; i<len/2; i++) if(Str[i]! = str[len-i-1]) return 0; return 1;}intMain () {intn,i,j,t; intT1,flag; Chartemp[2] = {' /'}; scanf ("%d%s",&n,&str); Len=strlen (str); J=0; Flag=0;if(n = = -) { for(t=0;t< -; t++)//palindrome number within 30 steps { if(!Ispalindrome ()) { for(i=0; i<len;i++) {T1=0; if(str[i]>='0'&& Str[i] <='9') T1+ = (Str[i]-'0'); ElseT1+ = (Str[i]-'A'+Ten); if(str[len-i-1]>='0'&& str[len-i-1] <='9') T1+ = (str[len-i-1] -'0'); ElseT1+ = (str[len-i-1] -'A'+Ten); T1+=Flag; if(T1 >= N)//to be rounded up { if(t1-n<=9) itoa (T1-n,temp,Ten);//integral type converted to character type Elsetemp[0] ='A'+ (t1-n-Ten); Flag=1; Str1[j+ +] = temp[0]; } Else{ if(t1<=9) itoa (T1,temp,Ten);//integral type converted to character type Elsetemp[0] ='A'+ (t1-Ten); Flag=0; Str1[j+ +] = temp[0]; } } if(flag) {Str1[j]='1'; Str1[j+1] =' /'; Flag=0;//Place 0 } ElseStr1[j]=' /'; strcpy (STR,STR1);//Assigning a str1 to strLen =strlen (str); J=0; } Else{printf ("step=%d\n", T); Break; } } if(T = = -) printf ("impossible!\n"); } Else{ for(t=0;t< -; t++)//palindrome number within 30 steps { if(!Ispalindrome ()) { for(i=0; i<len;i++) {T1= (str[i]-'0') + (str[len-i-1] -'0') + flag;//to add flag if(T1 >= N)//to be rounded up{itoa (T1-n,temp,Ten);//integral type converted to character typeFlag =1; Str1[j+ +] = temp[0]; } Else{itoa (t1,temp,Ten);//integral type converted to character typeFlag =0; Str1[j+ +] = temp[0]; } } if(flag) {Str1[j]='1'; Str1[j+1] =' /'; Flag=0;//Place 0 } ElseStr1[j]=' /'; strcpy (STR,STR1);//Assigning a str1 to strj =0; Len=strlen (str); } Else{printf ("step=%d\n", T); Break; } } if(T = = -) printf ("impossible!\n"); } return 0;}
Algorithm Training palindrome Number