以下代码是用于找到数组中最大数的计数。但是它没有起作用。

huangapple go评论88阅读模式
英文:

The Below code is to find the count of highest number in a array. but it is not working

问题

我正在尝试使用以下GO代码来获取最大数字计数,但似乎不起作用。这是一个简单的程序,但我无法得到正确的答案,即在这种情况下是2。

package main

import (
	"fmt"
)

func main() {
	fmt.Print(findLargeNumberCount([]int32{4, 4, 1, 3}))
}

func findLargeNumberCount(candles []int32) int32 {
	var highest int32 = 0
	var prev int32 = candles[0]
	for i := 1; i < len(candles); i++ {
		if prev == candles[i] {
			highest++
		} else if candles[i] > prev {
			highest = 1
			
		}
		prev = candles[i]
	}
	return highest
}
英文:

I am trying to get a highest number count using GO CODE below but i seems to be not working. which is a simple program but i was unable to get the correct answer which is 2 in this case.

package main

import (
	&quot;fmt&quot;
)

func main() {
	fmt.Print(findLargeNumberCount([]int32{4, 4, 1, 3}))
}

func findLargeNumberCount(candles []int32) int32 {
	var highest int32 = 0
	var prev int32 = candles[0]
	for i := 1; i &lt; len(candles); i++ {
		if prev == candles[i] {
			highest++
		} else if candles[i] &gt; prev {
			highest = 1
			
		}
		prev = candles[i]
	}
	return highest
}

答案1

得分: 2

你应该修改你的代码以使其正常工作:

package main

import (
	"fmt"
)

func main() {
	fmt.Print(findLargeNumberCount([]int32{4, 4, 1, 3}))
}

func findLargeNumberCount(candles []int32) int32 {
	var highest int32 = 1
	var prev int32 = candles[0]
	for i := 1; i < len(candles); i++ {
		if prev == candles[i] {
			highest++
		} else if candles[i] > prev {
			prev = candles[i]
			highest = 1

		}
	}
	return highest
}

https://go.dev/play/p/0OnUiGnfKXI

修改内容如下:
1)var highest int32 = 1,而不是0
2)prev = candles[i] 应该在 else if 块中

解释:
假设切片的第一个元素(代码中的 candles)是最大的(代码中的 prev),并将最大数字的计数设置为1(代码中的 highest)。

然后开始遍历切片:

如果我们有一个元素(代码中的 candles[i])等于当前最大值(prev),那么我们增加最大数字的计数(highest)。

否则,如果我们有一个元素(candles[i])大于当前最大值(prev),我们将新的当前最大值赋值为 prev = candles[i],并将最大数字的计数重置为1(highest = 1),然后继续迭代直到切片的末尾。

英文:

You should change your code to have it works:

package main

import (
	&quot;fmt&quot;
)

func main() {
	fmt.Print(findLargeNumberCount([]int32{4, 4, 1, 3}))
}

func findLargeNumberCount(candles []int32) int32 {
	var highest int32 = 1
	var prev int32 = candles[0]
	for i := 1; i &lt; len(candles); i++ {
		if prev == candles[i] {
			highest++
		} else if candles[i] &gt; prev {
			prev = candles[i]
			highest = 1

		}
	}
	return highest
}

https://go.dev/play/p/0OnUiGnfKXI

What was changed:

  1. var highest int32 = 1 , not 0
  2. prev = candles[i] should be in else if block

Explanation:

Assume, that the first element of the slice(candles in code) is the highest one (prev in code) and set the count of the highest number to 1 (highest in code).

After that start iterating over the slice:

If we have an element of the slice (candles[i] in code) that is equal to the current highest(prev) then we increase the highest number count (highest),

else if we have an element (candles[i]) that is higher than the current highest (prev) we assign new current highest prev = candles[i] and reset the highest number count to 1 highest = 1 and keep iterating till the end of the slice.

huangapple
  • 本文由 发表于 2021年12月23日 19:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70461547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定