题解 | #记负均正II#
记负均正II
http://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
//题不难,细心最重要,注意分情况判断,while处理输入,输出语句需要放外面
import java.util.*; public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//count0表示0的个数,count1表示负数的个数,count2表示非负数的个数, sum表示非负数的和
int count0 = 0, count1 = 0, count2 = 0, sum = 0;
while(sc.hasNext()){
int num = sc.nextInt();
if(num < 0){
count1++;
}else if(num == 0){
count0++;
sum += num;
count2++;
}else{
sum += num;
count2++;
}
}
if(count0 == count2){ //表示没有正数
System.out.println(count1 + "\n" + "0.0" + "\n");
}else{
System.out.println(count1 + "\n" + String.format("%.1f", sum * 1.0 / count2) +"\n");
}
}
}