题解 | #记负均正#
记负均正
https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8
package main
import (
"fmt"
"bufio"
"os"
"strings"
"strconv"
)
func process(nums []int)(int, float32){
n := len(nums)
total := 0
sum := 0
num := 0
for i := 0; i < n; i++{
if nums[i] < 0{
total++
}else if nums[i] > 0{
sum += nums[i]
num++
}
}
if sum == 0{
return total, 0.0
}
return total, float32(sum) / float32(num)
}
func main(){
var n int
fmt.Scanln(&n)
nums := make([]int, n)
reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n')
str = str[:len(str) - 1]
strs := strings.Split(str, " ")
for i := 0; i < n; i++{
num := 0
if strs[i][0] == '-'{
num, _ = strconv.Atoi(strs[i][1:])
num *= -1
}else{
num, _ = strconv.Atoi(strs[i])
}
nums[i] = num
}
a, b := process(nums)
fmt.Printf("%d %0.1f", a, b)
}
import (
"fmt"
"bufio"
"os"
"strings"
"strconv"
)
func process(nums []int)(int, float32){
n := len(nums)
total := 0
sum := 0
num := 0
for i := 0; i < n; i++{
if nums[i] < 0{
total++
}else if nums[i] > 0{
sum += nums[i]
num++
}
}
if sum == 0{
return total, 0.0
}
return total, float32(sum) / float32(num)
}
func main(){
var n int
fmt.Scanln(&n)
nums := make([]int, n)
reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n')
str = str[:len(str) - 1]
strs := strings.Split(str, " ")
for i := 0; i < n; i++{
num := 0
if strs[i][0] == '-'{
num, _ = strconv.Atoi(strs[i][1:])
num *= -1
}else{
num, _ = strconv.Atoi(strs[i])
}
nums[i] = num
}
a, b := process(nums)
fmt.Printf("%d %0.1f", a, b)
}