题目
HJ105 记负均正II
golang 实现
package mainimport ("bufio""fmt""io""os""strconv""strings"
)func main() {scanner := bufio.NewScanner(os.Stdin)nums:=make([]int,0)sum:=0minus:=0for scanner.Scan() {line := scanner.Text()fields := strings.Fields(line)for _,field:=range fields{num,_:=strconv.Atoi(field)if num <0{minus=minus+1}else{nums = append(nums,num)sum = sum+num}}}if err := scanner.Err(); err != nil {if err != io.EOF {fmt.Fprintln(os.Stderr, err)}}numsLen:=len(nums)if numsLen==0{fmt.Printf("%d\n%.1f",minus, 0.0)} else{avg:=float64(sum)/float64(numsLen)// value:=strconv.FormatFloat(avg, 'f', 1, 64)fmt.Printf("%d\n%.1f",minus,avg )}
}