C language example question question, solve?!

Updated on educate 2024-02-09
8 answers
  1. Anonymous users2024-02-05

    This is very easy to understand, let me give you a number, take n=5, although the compiler has an execution order, but theoretically it can be considered that s1 and s2 are executed at the same time, and look at it in comparison.

    The main function calls fibona(5); Then n

    Also. =2, so:

    s1:1 executes s1=fibona(n-1);

    At this point, n becomes 4, and fibona(4) is called;

    2》n!=1 also! =2, run s1=fibona(n-1);

    At this point, n becomes 3, and fibona(3) is called;

    3》n!=1 also! =2, run s1=fibona(n-1);

    At this time, n becomes 2, then n-1=1 is returned, so s1=1,4" continues to call fibona(n-1); i.e. fibona (2);

    5》At this time, n becomes 1, and fibona(1) is called;

    returns n-1=1, so s1=1,6" because n is 1 and n-1 is 0, s1 stops calling fibona(n-1);

    s2:1" executes s1=fibona(n-2);

    At this point, n becomes 4, and fibona(3) is called;

    2》n!=1 also! =2, run s1=fibona(n-2);

    At this point, n becomes 1, and fibona(1) is called;

    returns n-1=1, so s2=1;

    Since n-2<0, s2 stops computation.

    The second call of s2 is over, s2 1, and the third call of s1 gives s1 1, so at this time s1 + s2 2

    The fourth call on s1 returns 1, so s1+s2 becomes 3.

  2. Anonymous users2024-02-04

    The structure of the whole program is to use a function call to find the fibonacci function: if(n==1||n==2)

    return(n-1);

    s1=fibona(n-1);

    s2=fibona(n-2);

    return(s1+s2);

    This means that if n==1 or n==2, n-1 is returned, i.e., n==1 returns 0n==2

    Returning 1 for numbers that are neither 1 nor 2 is executed:

    s1=fibona(n-1);

    s2=fibona(n-2);

    return(s1+s2);

    The above three sentences are a recursive implementation

  3. Anonymous users2024-02-03

    When n=1 or n=2 occurs at the end of the call, the values of the first two terms of the fibonacci series are returned to the previous call, and then passed back to the first call.

    s1=fibona(n-1);

    s2=fibona(n-2);

    return(s1+s2);

    These three sentences are.

    fibonacci(n)

    fibonacci(n-1)

    Embodiment of fibonacci (n-2).

    The whole solution is a recursive process.

  4. Anonymous users2024-02-02

    if(n==1||n==2)

    return(n-1)

    These two sentences say that if n==1 returns 0, n==2 returns 1, that is, the first two terms of the sequence are 0 and 1;

    s1=fibona(n-1);

    s2=fibona(n-2);

    These 2 sentences are to save the first 2 terms of one term of the required sequence, if the third term is required, then s1 = the second term, s2 = the second term, they are respectively called fibona function to find out;

    return(s1+s2);

    Find the latter term of the term represented by s1.

  5. Anonymous users2024-02-01

    This is a recursive method, which is more difficult to explain, let me try is known by the number sequence, the first term is 1, the second term is 1, and the third term is 2...

    From the third term onwards, each term is the sum of the first two terms, so, if(n==1||n==2)return(n-1)

    That is, when you find the value of the first or second term, it returns n 1, but I guess you are wrong here, it should be returned 1, because the first and second terms are both 1

    s1=fibona(n-1);

    s2=fibona(n-2);

    return(s1+s2);

    When the first term and the second term are not required, then its value is the sum of the first two terms, so let two variables, s1, s2, and s1 store the value of the previous term, and s2 store the first term of s1, and then return the sum of the two.

  6. Anonymous users2024-01-31

    Fibonacci is 0, 1, 1, 2, 3, 5 ,..ai,a(i+1),ai+a(i+1),.

    if(n==1||n==2)

    return(n-1);

    This sentence is to find the initial value!

    Each of the last terms is the sum of the first two! So first calculate the first two items, and then add the two items, and that's the result.

  7. Anonymous users2024-01-30

    The recursive method is more difficult to explain, the landlord you algebraically count into it, and then draw it on the paper, and you will understand it once you go.

  8. Anonymous users2024-01-29

    In the first question, the zhi:dao working variable in the question means a temporary variable, so it belongs to the second question: not only numeric characters are supported, but also other types of characters are supported. So wrong.

    Question 3: Correct, that's an infinite loop, unless you end up in a loop.

Related questions
4 answers2024-02-09

It's not very hard that the tail node next points to null. >>>More