给定三个数字。任务是在给定的三个数字中找到最小的数字。
例子:
Input: first = 15, second = 16, third = 10
Output: 10
Input: first = 5, second = 3, third = 6
Output: 3
方法:
- 检查第一个元素是否小于第二个和第三个元素。如果是,则打印它。
- 否则,检查第二个元素是否小于第一个和第三个元素。如果是,则打印它。
- 其他三分之一是最小的元素并将其打印出来。
下面是上述方法的实现:
C++
// C++ implementation to find
// the smallest of three elements
#include
using namespace std;
int main()
{
int a = 5, b = 7, c = 10;
if (a <= b && a <= c)
cout << a << " is the smallest";
else if (b <= a && b <= c)
cout << b << " is the smallest";
else
cout << c << " is the smallest";
return 0;
}
Java
// Java implementation to find
// the smallest of three elements
import java.io.*;
class GFG {
public static void main (String[] args) {
int a = 5, b = 7, c = 10;
if (a <= b && a <= c)
System.out.println( a + " is the smallest");
else if (b <= a && b <= c)
System.out.println( b + " is the smallest");
else
System.out.println( c + " is the smallest");
}
}
// This code is contributed by shs..
Python3
# Python implementation to find
# the smallest of three elements
a, b, c = 5, 7, 10
if(a <= b and a <= c):
print(a, "is the smallest")
elif(b <= a and b <= c):
print(b, "is the smallest")
else:
print(c, "is the smallest")
# This code is contributed
# by 29AjayKumar
C#
// C# implementation to find
// the smallest of three elements
using System;
class GFG
{
static public void Main ()
{
int a = 5, b = 7, c = 10;
if (a <= b && a <= c)
Console.WriteLine( a + " is the smallest");
else if (b <= a && b <= c)
Console.WriteLine( b + " is the smallest");
else
Console.WriteLine( c + " is the smallest");
}
}
// This code is contributed by jit_t
PHP
输出:
5 is the smallest
时间复杂度: O(1)