题目链接:https://www.dotcpp.com/oj/problem2060.html
题目解析:对给定数据按升序排序,假设数组 arr[] 长度为n,取数组前 n / 2 + 1 个元素,然后对每一个元素取 arr[i] /2 +1,累加即可得到答案。
注:在这里不需要考虑 n 为偶数或奇数,因为如果 n = 4,那么 n / 2 为 2,题目要求是大于一半,所以要取 n / 2 + 1。当 n = 5时,由于默认是向下取整,所以必然是 n / 2 +1。
代码
import java.util.*;public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {int n = sc.nextInt();if(n == 0){return;}int[] people = new int[n];for(int i = 0; i < n; i++){people[i] = sc.nextInt();}Arrays.sort(people);int ans = 0;for(int i = 0; i <= n/2; i++){ans += people[i] / 2 + 1;}System.out.println(ans);}}}
