You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package cap3
|
|
|
|
|
|
|
|
type Ex27Condition int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Ex27ConditionMin Ex27Condition = iota
|
|
|
|
Ex27ConditionMax
|
|
|
|
)
|
|
|
|
|
|
|
|
func Ex27(nums []int) (max1, max2, min1, min2 int) {
|
|
|
|
size := len(nums)
|
|
|
|
if size < 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 最大的乘积
|
|
|
|
maxProd := nums[size-1] * nums[1]
|
|
|
|
max1, max2 = nums[size-1], nums[1]
|
|
|
|
|
|
|
|
// 最小的乘积
|
|
|
|
minProd := nums[size-1] * nums[1]
|
|
|
|
min1, min2 = nums[size-1], nums[1]
|
|
|
|
|
|
|
|
for i := 1; i < size; i++ {
|
|
|
|
first, second := (size+i-1)%size, (i+1)%size
|
|
|
|
// 当前的乘积
|
|
|
|
currentProd := nums[first] * nums[second]
|
|
|
|
|
|
|
|
// 找出最大最小的乘积
|
|
|
|
if currentProd > maxProd {
|
|
|
|
maxProd = currentProd
|
|
|
|
max1, max2 = nums[first], nums[second]
|
|
|
|
} else if currentProd < minProd {
|
|
|
|
minProd = currentProd
|
|
|
|
min1, min2 = nums[first], nums[second]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max1, max2, min1, min2
|
|
|
|
}
|