题解 | #记负均正#
记负均正
https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8
思路:遍历每一个数,是负数,负数个数加一,正数则求和和正数个数加一,代码如下:
import java.util.*; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { // Scanner in = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; // 注意 hasNext 和 hasNextLine 的区别 while ((str = br.readLine()) != null) { // 注意 while 处理多个 case int n = Integer.parseInt(str); String[] s = br.readLine().split(" "); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } double avg = 0.0; int sum = 0; int positive_num = 0; int negative_num = 0; for (int i = 0; i < n; i++) { if (arr[i] < 0) { negative_num++; } else if (arr[i] != 0) { positive_num++; sum += arr[i]; } } if (sum != 0) avg = (double) sum / positive_num; avg = (sum != 0) ? (double) sum / positive_num : avg; System.out.println(negative_num + " " + (double)Math.round(avg * 10) / 10); // System.out.println(negative_num + " " + // String.format("%.1f", avg)); } } }