But all four groups of test data is wrong answer,
Hope to have a great god can help you see, the grateful orz
The first set of test data:
2
10011
Topics are as follows:
Title description
If a number (the first is not zero) read from right to left and from left to right are the same, we call it the palindrome,
For example: given a decimal number 56, add 56 to 65 (that is, the 56 read from right to left), is a palindrome, you get 121
Such as: for the decimal number 87:
STEP1:87 + 78=165
STEP2:165 + 561=726
STEP3:726 + 627=1353
STEP4:1353 + 3531=4884
Step here refers to an N in addition, the patients use the four steps to get at least 4884, palindrome
Write a program that, given a N (2 N 10 or less or less or N=16) hexadecimal number M within (100), at least after a few steps can get a palindrome, if within 30 steps (including step 30) may not get palindrome, output is Impossible! ,
Input format
Two lines, N, M,
The output format
If can get palindrome within 30 steps, output formats such as STEP=ans, the ans steps to at least get a palindrome,
Otherwise the output Impossible! ,
the building code is as follows:
# include
Int judge (int * p, int bit);
Int main ()
{
Int n;
The scanf (" % d \ n ", & amp; N);
char ch;
Int I=0, j, carry=0;//counter I, j and remainder carry
Int a [200], [200] b;
While ((ch=getchar ())! )
='\ n'{
If (ch>='0' & amp; & ChElse a [i++]=ch - 'a' + 10;
}//cycle after a [I - 1) is the final number
For (j=0; J<=I - 1; + + j)
B [j]=[] I - j - 1;//to reverse a
If (judge (a, I))//if the input palindrome, then output the number of 0.
Printf (" STEP=0 \ n ");
The else
{
Int c [200], bit=I, ans=0;//I, but, pay attention to the array first is [0];
While (1)
{
For (j=0;; J++)
{
C [j]=(a + b [j] [j] + carry) % n;
If (a + b [j] [j] + carry>=n)//determine whether have carry
Carry=1;
The else
Carry=0;
If (j==bit - 1)//if the last
{
If (carry==1)
{
Bit +=1;//digits plus 1;
C [bit - 1]=1;//first into 1
Carry=0;//remainder reset
}
break;
}
}
+ + ans;//the number 1
If (judge (c, a bit))//judge whether palindrome
{
Printf (" STEP=% d \ n ", ans);
return 0;
}
If (ans> 30)//if the number is greater than 30
{
Printf (" Impossible! \n");
return 0;
}
For (j=0; J<=bit - 1; J++)//redefine ABC
{
A [j]=c [j];
B] [bit - j - 1=c [j];
}
}
}
return 0;
}
Int judge (int * p, int bit)//judge whether palindrome
{
int i;
for(i=0; I<=bit - 1; I++)
{
If (p [I]!=p -i] [bit - 1)
return 0;
}
return 1;
}
CodePudding user response:
C [j]=(a + b [j] [j] + carry) % n;If the input is the hex you're wrong
A [j] is' a '
You calculate (' a '+' a ') % 16?
CodePudding user response:
More than a decimal your algorithm is wrongCodePudding user response:
Oh, I see wrongIf (ch>='0' & amp; & ChElse a [i++]=ch - 'a' + 10;
Have to do special treatment
CodePudding user response:
There are holes, 44 when ans=31 still output, is amended as:if (judge (c, bit) & amp; & Ans<=30)//judge whether palindrome
CodePudding user response:
I see, is likely to be the output STEP=31