我们经常需要找出两个数中的最大值。通常,我们会使用 if
来实,但是如果多的话肯定就不好使了。所以这里从两个数的最大值和最小值公式出发,推出如何求解
n
n
n 个数的最大值和最小值,并提供cpp、go、爪哇的代码。
1. 两个数的MAX
给定两个数 a a a 和 b b b,它们的最大值可以通过以下公式计算:
max ( a , b ) = ( a + b + ∣ a − b ∣ ) 2 \text{max}(a, b) = \frac{(a + b + |a - b|)}{2} max(a,b)=2(a+b+∣a−b∣)
如果
a
>
b
a > b
a>b,则
∣
a
−
b
∣
=
a
−
b
|a - b| = a - b
∣a−b∣=a−b,则:
max
(
a
,
b
)
=
(
a
+
b
+
(
a
−
b
)
)
2
=
a
\text{max}(a, b) = \frac{(a + b + (a - b))}{2} = a
max(a,b)=2(a+b+(a−b))=a
如果
a
≤
b
a \leq b
a≤b,则
∣
a
−
b
∣
=
b
−
a
|a - b| = b - a
∣a−b∣=b−a,则:
max
(
a
,
b
)
=
(
a
+
b
+
(
b
−
a
)
)
2
=
b
\text{max}(a, b) = \frac{(a + b + (b - a))}{2} = b
max(a,b)=2(a+b+(b−a))=b
2. 两个数的MIN
类似地,两个数 a a a 和 b b b 的最小值可以用以下公式计算:
min ( a , b ) = ( a + b − ∣ a − b ∣ ) 2 \text{min}(a, b) = \frac{(a + b - |a - b|)}{2} min(a,b)=2(a+b−∣a−b∣)
如果
a
<
b
a < b
a<b,则
∣
a
−
b
∣
=
b
−
a
|a - b| = b - a
∣a−b∣=b−a,代入公式得:
min
(
a
,
b
)
=
(
a
+
b
−
(
b
−
a
)
)
2
=
a
\text{min}(a, b) = \frac{(a + b - (b - a))}{2} = a
min(a,b)=2(a+b−(b−a))=a
如果
a
≥
b
a \geq b
a≥b,则
∣
a
−
b
∣
=
a
−
b
|a - b| = a - b
∣a−b∣=a−b,代入公式得:
min
(
a
,
b
)
=
(
a
+
b
−
(
a
−
b
)
)
2
=
b
\text{min}(a, b) = \frac{(a + b - (a - b))}{2} = b
min(a,b)=2(a+b−(a−b))=b
3. 扩展到 n n n 个数
求 n n n 个数 m a x \text max max :
max ( a 1 , a 2 , . . . , a n ) = max ( max ( a 1 , a 2 ) , a 3 , . . . , a n ) \text{max}(a_1, a_2, ..., a_n) = \text{max}(\text{max}(a_1, a_2), a_3, ..., a_n) max(a1,a2,...,an)=max(max(a1,a2),a3,...,an)
求
n
n
n 个数
m
i
n
\text min
min :
min
(
a
1
,
a
2
,
.
.
.
,
a
n
)
=
min
(
min
(
a
1
,
a
2
)
,
a
3
,
.
.
.
,
a
n
)
\text{min}(a_1, a_2, ..., a_n) = \text{min}(\text{min}(a_1, a_2), a_3, ..., a_n)
min(a1,a2,...,an)=min(min(a1,a2),a3,...,an)
4. 代码
4.1 cpp
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double maxTwo(double a, double b) {
return (a + b + abs(a - b)) / 2;
}
double minTwo(double a, double b) {
return (a + b - abs(a - b)) / 2;
}
double maxN(const vector<double>& nums) {
if (nums.empty()) return 0;
double curMax = nums[0];
for (size_t i = 1; i < nums.size(); ++i) {
curMax = maxTwo(curMax, nums[i]);
}
return curMax;
}
double minN(const vector<double>& nums) {
if (nums.empty()) return 0;
double curMin = nums[0];
for (size_t i = 1; i < nums.size(); ++i) {
curMin = minTwo(curMin, nums[i]);
}
return curMin;
}
int main() {
vector<double> nums = {5, 3, 8, 6, 2};
double _max = maxN(nums);
double _min = minN(nums);
cout << "max: " << _max << endl;
cout << "min: " << _min << endl;
return 0;
}
4.2 Go
package main
import (
"fmt"
"math"
)
func maxTwo(a, b float64) float64 {
return (a + b + math.Abs(a-b)) / 2
}
func minTwo(a, b float64) float64 {
return (a + b - math.Abs(a-b)) / 2
}
func maxN(nums []float64) float64 {
if len(nums) == 0 {
return 0
}
curMax := nums[0]
for _, num := range nums[1:] {
curMax = maxTwo(curMax, num)
}
return curMax
}
func minN(nums []float64) float64 {
if len(nums) == 0 {
return 0
}
curMin := nums[0]
for _, num := range nums[1:] {
curMin = minTwo(curMin, num)
}
return curMin
}
func main() {
nums := []float64{1, 9, 2, 5, 8}
_max := maxN(nums)
_min := minN(nums)
fmt.Println("MAX:", _max)
fmt.Println("MIN:", _min)
}
4.3 Java
public class Main {
public static double maxTwo(double a, double b) {
return (a + b + Math.abs(a - b)) / 2;
}
public static double minTwo(double a, double b) {
return (a + b - Math.abs(a - b)) / 2;
}
public static double maxN(double[] nums) {
if (nums.length == 0) return 0;
double curMax = nums[0];
for (int i = 1; i < nums.length; i++) {
curMax = maxTwo(curMax, nums[i]);
}
return curMax;
}
public static double minN(double[] nums) {
if (nums.length == 0) return 0;
double curMin = nums[0];
for (int i = 1; i < nums.length; i++) {
curMin = minTwo(curMin, nums[i]);
}
return curMin;
}
public static void main(String[] args) {
double[] nums = {11, 13, 8, 6, 12};
double _max = maxN(nums);
double _min = minN(nums);
System.out.println("max: " + _max);
System.out.println("min: " + _min);
}
}