Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Write a recursive function that gets an int n and another number f that is between 0 and 3, the function returns 0 or 1 depending on the following conditions:

  1. If f=0, the function will return 1 if the sequence (from right to left) is an ascending series Otherwise it will return 0.

  2. If f=1, the function will return 1 if the sequence (from right to left) is a descending series Otherwise it will return 0.

  3. If f=2, the function will return 1 if the sequence (from right to left) is an ascending series up to a certain number and then it's descending (up to the end of the number) Otherwise it will return 0.

  4. If f=3, the function will return 1 if the sequence (from right to left) is a descending series up to a certain number and then it's ascending (up to the end of the number) Otherwise it will return 0.

  5. If the number n has two consecutive same numbers, for every f value the function will return 0. For example, for the number 1223 for any f value, the function will return 0.

For example:

For 1234,0 the function returns 0

For 4321,0 the function returns 1

For 1234,1 the function returns 1

For 4321,1 the function returns 0

For 12341,2 the function returns 1

For 412341,2 the function returns 0

For 96589,2 the function returns 0.

For 12341,3 the function returns 0

For 96589,3 the function returns 1

For the number 1223 for any f value, the function will return 0.

I wrote this code but the case for f=2 always returns 1.

    #include <stdio.h>

    int func(int n, int f)
    {

    int zero = 0;
    int* one = &zero;

    if (n < 10)
        return n;

    if((func(n / 10, f) == n % 10)
        return 0;

    switch (f)
    {
    case(0):
        if (func(n / 10, f) > n % 10)     
            return (n % 10);
        else
                return 0;

     case(1):
            if (func(n / 10, f) < n % 10)
                return (n % 10);
            else
                return 0;

    
    case(2):
            if (func(n / 10, f) > n % 10)
                return (n % 10);
            else
                return 0;
                
            if (*one == 0)
                if (func(n / 10, f) < n % 10) 
                {
                    *one++;
                    return (n % 10);
                }
            
    case(3):
        if (func(n / 10, f) < n % 10)
            return (n % 10);
        else
            return 0;
        
        if (*one == 0)
            if (func(n / 10, f) > n % 10) 
            {
                *one++;
                return (n % 10);
            }
     }
    }
    
    int main(void)
    {
        
        //int n=1234, f=0; //should return 0
        //int n=4321, f=0; //should return 1
        //int n=1234, f=1; //should return 1
        //int n=4321, f=1; //should return 0
        //int n=12341, f=2; //should return 1
        //int n=412341, f=2; //should return 0
        //int n=96589, f=2;//should return 0
        //int n=96589, f=3; //should return 1
        //int n=12341, f=3; //should return 0
    
        if (func(n , f) != 0)
            printf("1
");
        else
            printf("0
");
        return 0;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
123 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are several issues with your code:

  • The function is supposed to return either 0 (false) or 1 (true), but you seem to use return value of the truth case for the actual digit at that recursion step. That could be okay if all digits were positive, because any value except for 0 is considered as "true", but I think 210 should return true for case 0.

  • In the cases 2 and 3, you have an additional condition:

        if (func(n / 10, f) > n % 10)
            return (n % 10);
        else
            return 0;
    
        if (*one == 0) ...  // dead code!
    

    The if on that condition won't be reached, because you have already returned 0 or n % 10 earlier. (You also don't catch all possible paths if these coditions, as Bob told you.)

  • Your pointer one will not buy you anything, because it is always bound to the same local variable, zero. All occurrences of one are dereferences (*one) and you could replace all of them with just zero. That means that if your condition (*one == 0) were actually reachable, it would always be true.

    I guess your intention was to have a flag that is visible in other recusion steps, but it doesn't work that way. For that to work, you should pass a pointer to some value, so that you can access the same data through it in all recursion steps.)

Okay, let's rewrite this function so that it always returns 0 or 1. First, set up some local variables for the current and next digits and catch the base cases:

int func(int n, int f)
{
    int p = n % 10;
    int q = (n / 10) % 10;

    if (n < 10) return (f < 2);
    if (p == q) return 0;

    // ...
    
    return 0;
}

Now let's consider the first two cases: if the two digits are out of order, you can return 0 immediately. Otherwise, recurse with the rest of the number. You can write this concisely with a single return statement, because the shot-circuiting && ensures that you never recurse when p < q or p > q are false:

    switch (f) {
    case 0:     return (p < q && func(n / 10, f));
    case 1:     return (p > q && func(n / 10, f));
    // ...
    }

Now the other two cases with a peak. Test your conditions as above. When they fail, you have reached the peak and the rest of the digits must be decreasing. Fortunately, you already have a function to check that, nemale cases 0 and 1. So test and recurse while the initial condition holds, otherwise call the appropriate function on the remaining number:

    switch (f) {
    case 0:     return (p < q && func(n / 10, f));
    case 1:     return (p > q && func(n / 10, f));
    case 2:     return (p < q && func(n / 10, f)) || func(n, 1);
    case 3:     return (p > q && func(n / 10, f)) || func(n, 0);
    }

This implementation will treat strictly monotonic numbers which satisfy eiter case 0 or 1 as special cases of cases 2 and 3, where the peak or valley is at the end.

Note that since none of the tests in the switch allow equality, the if (p == q) return 0; is redundant.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...