使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP