📜  三分之二使用最小比较

📅  最后修改于: 2021-04-27 23:57:53             🧑  作者: Mango

给定三个不同的数字a,b和c,找出中间值的数字。
例子 :

Input : a = 20, b = 30, c = 40
Output : 30

Input : a = 12, n = 32, c = 11
Output : 12

简单方法:

C++
// CPP program to find  middle of three distinct
// numbers
#include 
using namespace std;
 
int middleOfThree(int a, int b, int c)
{
    // Checking for b
    if ((a < b && b < c) || (c < b && b < a))
       return b;
 
    // Checking for a
    else if ((b < a && a < c) || (c < a && a < b))
       return a;
 
    else
       return c;
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java
// Java program to find middle of
// three distinct numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Checking for b
        if ((a < b && b < c) || (c < b && b < a))
            return b;
 
        // Checking for a
        else if ((b < a && a < c) || (c < a && a < b))
        return a;
 
        else
        return c;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to find middle
# of three distinct numbers
 
def middleOfThree(a, b, c):
     
    # Checking for b
    if ((a < b and b < c) or (c < b and b < a)) :
        return b;
 
    # Checking for a
    if ((b < a and a < c) or (c < a and a < b)) :
        return a;
 
    else :
        return c
 
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
 
# This code is contributed by rishabh_jain


C#
// C# program to find middle of
// three distinct numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                        int c)
    {
        // Checking for b
        if ((a < b && b < c) || (c < b && b < a))
            return b;
 
        // Checking for a
        else if ((b < a && a < c) || (c < a && a < b))
        return a;
 
        else
        return c;
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// CPP program to find middle of three distinct
// numbers
#include 
using namespace std;
 
// Function to find the middle of three numbers
int middleOfThree(int a, int b, int c)
{
    // Compare each three number to find middle
    // number. Enter only if a > b
    if (a > b)
    {
        if (b > c)
            return b;
        else if (a > c)
            return c;
        else
            return a;
    }
    else
    {
        // Decided a is not greater than b.
        if (a > c)
            return a;
        else if (b > c)
            return c;
        else
            return b;
    }
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java
// Java program to find middle of
// three distinct numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three numbers
def middleOfThree(a, b, c) :
     
    # Compare each three number to find
    # middle number. Enter only if a > b
    if a > b :
        if (b > c):
            return b
        elif (a > c) :
            return c
        else :
            return a
    else:
        # Decided a is not greater than b.
        if (a > c) :
            return a
        elif (b > c) :
            return c
        else :
            return b
 
# Driver Code
a = 20
b = 30
c = 40
print( middleOfThree(a, b, c) )
 
# This code is contributed by rishabh_jain


C#
// C# program to find middle of
// three distinct numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                            int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by vt_m


PHP
 b
    if ($a > $b)
    {
        if ($b > $c)
            return $b;
        else if ($a > $c)
            return $c;
        else
            return $a;
    }
    else
    {
         
        // Decided a is not
        // greater than b.
        if ($a > $c)
            return $a;
        else if ($b > $c)
            return $c;
        else
            return $b;
    }
}
 
    // Driver Code
    $a = 20; $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>


C++
// CPP program to find middle of three distinct
// numbers
#include 
using namespace std;
 
// Function to find the middle of three number
int middleOfThree(int a, int b, int c)
{
    // x is positive if a is greater than b.
    // x is negative if b is greater than a.
    int x = a - b;
 
    int y = b - c;  // Similar to x
    int z = a - c;  // Similar to x and y.
 
    // Checking if b is middle (x and y both
    // are positive)
    if (x * y > 0)
        return b;
 
    // Checking if c is middle (x and z both
    // are positive)
    else if (x * z > 0)
        return c;
    else
        return a;
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}


Java
//java program to find middle of three distinct
// numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
 
        int y = b - c; // Similar to x
        int z = a - c; // Similar to x and y.
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three number
def middleOfThree(a, b, c) :
     
    # x is positive if a is greater than b.
    # x is negative if b is greater than a.
    x = a - b
 
    # Similar to x
    y = b - c
     
    # Similar to x and y.
    z = a - c
 
    # Checking if b is middle (x and y
    # both are positive)
    if x * y > 0:
        return b
 
    # Checking if c is middle (x and z
    # both are positive)
    elif (x * z > 0) :
        return
    else :
        return a
 
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
 
# This code is contributed by rishabh_jain


C#
//C# program to find middle of three distinct
// numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                        int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
         
        // Similar to x
        int y = b - c;
         
         // Similar to x and y.
        int z = a - c;
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by vt_m


PHP
 0)
        return $b;
 
    // Checking if c is
    // middle (x and z both
    // are positive)
    else if ($x * $z > 0)
        return $c;
    else
        return $a;
}
 
    // Driver Code
    $a = 20;
    $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

30

但是由于额外的比较,上面使用的方法效率不高,可以轻松地将其最小化。如果第一部分为假,它将执行剩余的一半以检查条件。如果我们检查也是这个问题仍然相同。
更好的方法(需要更少的比较):

C++

// CPP program to find middle of three distinct
// numbers
#include 
using namespace std;
 
// Function to find the middle of three numbers
int middleOfThree(int a, int b, int c)
{
    // Compare each three number to find middle
    // number. Enter only if a > b
    if (a > b)
    {
        if (b > c)
            return b;
        else if (a > c)
            return c;
        else
            return a;
    }
    else
    {
        // Decided a is not greater than b.
        if (a > c)
            return a;
        else if (b > c)
            return c;
        else
            return b;
    }
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}

Java

// Java program to find middle of
// three distinct numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three numbers
def middleOfThree(a, b, c) :
     
    # Compare each three number to find
    # middle number. Enter only if a > b
    if a > b :
        if (b > c):
            return b
        elif (a > c) :
            return c
        else :
            return a
    else:
        # Decided a is not greater than b.
        if (a > c) :
            return a
        elif (b > c) :
            return c
        else :
            return b
 
# Driver Code
a = 20
b = 30
c = 40
print( middleOfThree(a, b, c) )
 
# This code is contributed by rishabh_jain

C#

// C# program to find middle of
// three distinct numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                            int c)
    {
        // Compare each three number to find
        // middle number. Enter only if a > b
        if (a > b)
        {
            if (b > c)
                return b;
            else if (a > c)
                return c;
            else
                return a;
        }
        else
        {
            // Decided a is not greater than b.
            if (a > c)
                return a;
            else if (b > c)
                return c;
            else
                return b;
        }
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine(middleOfThree(a, b, c));
    }
}
 
// This code is contributed by vt_m

的PHP

 b
    if ($a > $b)
    {
        if ($b > $c)
            return $b;
        else if ($a > $c)
            return $c;
        else
            return $a;
    }
    else
    {
         
        // Decided a is not
        // greater than b.
        if ($a > $c)
            return $a;
        else if ($b > $c)
            return $c;
        else
            return $b;
    }
}
 
    // Driver Code
    $a = 20; $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>

输出:

30

这种方法是有效的并且具有较少的比较次数。仅当a> b时才执行外部IF循环,否则将执行外部ELSE。
其他有效方法:

C++

// CPP program to find middle of three distinct
// numbers
#include 
using namespace std;
 
// Function to find the middle of three number
int middleOfThree(int a, int b, int c)
{
    // x is positive if a is greater than b.
    // x is negative if b is greater than a.
    int x = a - b;
 
    int y = b - c;  // Similar to x
    int z = a - c;  // Similar to x and y.
 
    // Checking if b is middle (x and y both
    // are positive)
    if (x * y > 0)
        return b;
 
    // Checking if c is middle (x and z both
    // are positive)
    else if (x * z > 0)
        return c;
    else
        return a;
}
 
// Driver Code
int main()
{
    int a = 20, b = 30, c = 40;
    cout << middleOfThree(a, b, c);
    return 0;
}

Java

//java program to find middle of three distinct
// numbers
import java.util.*;
 
class Middle
{  
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                          int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
 
        int y = b - c; // Similar to x
        int z = a - c; // Similar to x and y.
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int a = 20, b = 30, c = 40;
        System.out.println( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 program to find middle
# of three distinct numbers
 
# Function to find the middle of three number
def middleOfThree(a, b, c) :
     
    # x is positive if a is greater than b.
    # x is negative if b is greater than a.
    x = a - b
 
    # Similar to x
    y = b - c
     
    # Similar to x and y.
    z = a - c
 
    # Checking if b is middle (x and y
    # both are positive)
    if x * y > 0:
        return b
 
    # Checking if c is middle (x and z
    # both are positive)
    elif (x * z > 0) :
        return
    else :
        return a
 
# Driver Code
a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))
 
# This code is contributed by rishabh_jain

C#

//C# program to find middle of three distinct
// numbers
using System;
 
class Middle
{
    // Function to find the middle of three number
    public static int middleOfThree(int a, int b,
                                        int c)
    {
        // x is positive if a is greater than b.
        // x is negative if b is greater than a.
        int x = a - b;
         
        // Similar to x
        int y = b - c;
         
         // Similar to x and y.
        int z = a - c;
 
        // Checking if b is middle (x and y
        // both are positive)
        if (x * y > 0)
            return b;
 
        // Checking if c is middle (x and z
        // both are positive)
        else if (x * z > 0)
            return c;
        else
            return a;
    }
     
    // Driver code
    public static void Main()
    {
        int a = 20, b = 30, c = 40;
        Console.WriteLine( middleOfThree(a, b, c) );
    }
}
 
// This code is contributed by vt_m

的PHP

 0)
        return $b;
 
    // Checking if c is
    // middle (x and z both
    // are positive)
    else if ($x * $z > 0)
        return $c;
    else
        return $a;
}
 
    // Driver Code
    $a = 20;
    $b = 30;
    $c = 40;
    echo middleOfThree($a, $b, $c);
 
// This code is contributed by anuj_67.
?>

Java脚本


输出 :

30
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”