在C ++中使用最少比较的三分之三
在本节中,我们将看到如何比较三个给定值的中间值。因此,如果给出三个数字,如(10,30,20),则它将找到20,因为这是中间元素。首先让我们看一下算法,然后将其实现为C++代码。
算法
middle_of_three(a, b, c):
Input: Three numbers a, b and c
Output: The middle of these three
Begin
if a > b, then
if b > c, then
return b
else if a > c, then
return c
else
return a
else
if a > c, then
return a
else if b > c, then
return c
else
return b
End示例
#include <iostream>
using namespace std;
int mid_three(int a, int b, int c) {
if (a > b) {
if (b > c)
return b;
else if (a > c)
return c;
else
return a;
} else {
if (a > c)
return a;
else if (b > c)
return c;
else
return b;
}
}
main() {
int a = 10, b = 30, c = 20;
cout << "Middle Out of Three "<< mid_three(a, b, c);
}输出结果
Middle Out of Three 20
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练