题解 | #记负均正II#
记负均正II
https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
package main import ( "fmt" ) func calculate(nums []int) { var negCnt int var noNegCnt int var noNegSum int for _, num := range nums { if num < 0 { negCnt++ } else { noNegCnt++ noNegSum += num } } var avg float64 if noNegCnt == 0 { avg = 0.0 } else { avg = float64(noNegSum) / float64(noNegCnt) } fmt.Println(negCnt) fmt.Printf("%.1f\n", avg) } func main() { var nums []int for { var num int n, _ := fmt.Scan(&num) if n == 0 { break } nums = append(nums, num) } calculate(nums) }
// 本题输入为一行整数,所以采用:fmt.Scan(&num)