Home > Back-end > Using a recursive method to convert an integer n to string. Enter 256, for example, output "256
Using a recursive method to convert an integer n to string. Enter 256, for example, output "256
Time:10-23
3. #include Voidtranvers (intn) { If (n/10!=0) Tranvers (n/10); Printf (" % c ", 10 + n % '0'); } Intmain () { Intn; Printf (" input a integer: \ n "); The scanf (" % d ", & amp; N); Printf (" string is: "); If (n<0) { Printf (" - "); N=- n; } Tranvers (n); Return0; } this can look not to understand to understand
CodePudding user response:
The first call to a recursive function, n=256 n/10==25, recursive
Recursive function calls a second n=25, 10==n/2, recursive
Recursive function calls a third time, n=2 n/10==0, not recursive, print 10=2, 2% exit function
The third time after the function call, back to the second call code, then the code behind the print 10=5, 25% exit function
A second after the function call, back to the first call to the code, then the code behind the print 256% 10=6, exit function
The recursive function call is over, the main program exit
CodePudding user response:
Voidtranvers (intn) {tie the if (n/10!=0) Tranvers (n/10);//call this function first, is to deal with more than 10 parts, it will be recursive to output more than 10 Numbers Printf (" % c ", 10 + n % '0');//after the execution of this particular, it is after the Numbers in the output bits; Combined with a call before you get 256 this from what the output of the high order //put the above this two invert, get is the order of 652 }
In addition, also the questions several times, the satisfactory post have the check, always asking questions will not stick will be no one to give you a reply