C language-recursive Learning

Source: Internet
Author: User

I have never understood recursion before. Recently, school-enterprise cooperation has taught C language, and I just want to review it.
C, or sometimes I have to learn about it. When I was a freshman, I was really ignorant and confused. After I learned it, I felt like I could not do anything! Until the sophomore year is object-oriented
C ++ is a bit familiar, but it is not very open C and C ++. Let's get down to the truth and say recursion.

/* 1. */# include <stdio. h> int main () {unsigned long num = 0; printf ("\ N input an integer:"); scanf ("% lu", & num); // input, note % luprintf ("reverse output:"); unsigned long TMP = num; while (TMP! = 0) // end the loop {printf ("% lu", TMP % 10) when the result of dividing by 10 is a single digit ); // output the next TMP = tmp/10;} printf ("\ n"); Return 0;}/* 1. implement it recursively */# include <stdio. h> // use the B _2_ascii function to read the recursive example in the blog. Int B _2_ascii (unsigned int value) {unsigned int quotient; quotient = value/10; If (quotient! = 0) B _2_ascii (quotient); putchar (Value % 10 + '0'); Return 0;} void Rec (unsigned int N) {unsigned int value = N; printf ("% lu", Value % 10); value = N/10; If (Value % 10! = 0) Rec (value); return;} int main () {unsigned long num; printf ("input number:"); scanf ("% lu", & num ); rec (Num); B _2_ascii (Num); Return 0 ;}

First, you do not need to recursively implement reverse output. Naturally, you can divide by 10 and return the remainder of 10, for example, 123% 10 = 3; 123/10 = 12, 12% 10 = 2; 12/10 = 1,1% 10 = 1. (should be: 123
% 10 = 3,123/10 = 12;12% 10 = 2, 12/10 = 1;
1% 10 = 1, 1/10 = 1. In this way, the data is written in a unified manner, instead of outputting 3 as before, and then outputting the result after a loop .)
I am really a Junior. I don't even know how to implement recursion, So Baidu. See a blog recursive algorithm for detailed analysis-> C, the analysis is very good, the implementation of recursion is stack, the stack is advanced and later. So we can "help us print these characters in the correct order", but what is different from what I want to output in reverse order is that the example of this blog blogger needs to be output in positive order, because
The result of dividing 10 by 10 is the reverse order. Therefore, the blogger uses recursion to implement the reverse output, that is, the so-called advanced post-output.
However, what I want to achieve is positive output. I didn't come up with some explanations just now (so it is necessary to write a blog ). Although the stack is advanced and later, there are two opportunities to operate the data in the stack. (Although my data structure is not very good, I also know that the data can only be operated at the top of the stack)
1. At the top of the stack.
2. the stack is at the top of the stack.
If data is extracted and output at the top of the stack (before pushed to the bottom of the stack), the data is in the forward order. If output is taken out of the stack, It is output in reverse order.
Let's talk about my write error:
First, recursion can be said to constitute a loop, and I still write a while (Value % 10! = 0 ).
In addition, it is the choice of the conditions of the IF.

Finally, let's talk about an error in the previous blog. There is a saying in it: "using character constants instead of Integer constants can improve program portability." At that time, I stuck it to the QQ space. Later, some people asked why, I found that Baidu and Google keywords were usedSymbol constantIt can improve program portability ". Symbol constant
It is different from the character constant. It may be wrong for the blogger. Symbol constant example:
# Define PI 3.1415926
The blogger said that the character constant is 'A' and so on. As for why the use of symbolic constants can improve program portability, it is still not clear. My understanding is that it is easy to modify. You only need to modify one place for different platforms.
However, I am reminded again that I do not believe what others have said without adding a ticket.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.