题解 | #记负均正#题很简单,关键在于小数点后1位的展示
记负均正
https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int[] stn = new int[n];
int tmp = 0; //正数个数
int fg = 0; //负数个数
int sum = 0; //所有正数的和
double pvg = 0; //所有正数的和的平均值
for(int i = 0; i < n; i++){
stn[i] = in.nextInt();
if(stn[i] > 0){
sum += stn[i];
tmp++;
}else if(stn[i] < 0){
fg++;
}
}
if(tmp != 0){
pvg = sum / (tmp * 1.0);
}
System.out.println(fg + " " + String.format("%.1f",pvg));
}
}
}
查看15道真题和解析