首页 > 网易笔试,求教第三题哪错了
头像
麦茶叶蛋
编辑于 2020-08-08 16:43
+ 关注

网易笔试,求教第三题哪错了

太菜了,只A了第一题和第二题,第三题卡5%😥
public class Test15 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int q = 0; q < t; q++){
            int n = sc.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++)
                nums[i] = sc.nextInt();
            Arrays.sort(nums);
            // 找分界点
            // 打表
            int[] table = new int[n];
            int temp = 0;
            for (int j = 0; j < n; j++){
                temp = temp + nums[j];
                table[j] = temp;
            }
            int half = 0;
            int j = 0;
            for (j = n - 1; j > 0; j--){
                half = half + nums[j];
                if(half > table[j - 1])
                    break;
            }
            half = half - nums[j];
            System.out.println(table[n - 1] - 2 * half);
        }

    }
}


全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐