OJ-0715

示例1:

input
2 3 2 2 
output
7

示例2:

input
2 3 2 2 2
output
10

示例3:

input
2 3 2 2 5 2 5 4 3 3 4 2 2 2 2
output
24

题解

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] split = in.nextLine().split(" ");
        Map<Integer, Integer> map = new HashMap<>();
        for (String s : split) {
            int key = Integer.parseInt(s) + 1;
            int value = map.getOrDefault(Integer.parseInt(s) + 1, 0) + 1;
            map.put(key, value);
        }
        int sum = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            sum += (int) Math.ceil((double) entry.getValue() / entry.getKey()) * entry.getKey();
        }
        System.out.println(sum);
    }
}

参考

import java.util.Scanner;
 
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Integer,Integer> map = new HashMap<>();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            int key = num + 1 ;
            if(map.get(key)!=null){
                map.put(key,map.get(key)+1);
            }else{
                map.put(key,1);
            }
        }
        Set<Integer> set = map.keySet();
        int sum = 0 ;
        for(int k : set){
            int v = map.get(k);
            if(k<v){
                int x = v/k;
                if(v%k==0){
                    sum += x*k;
                }else{
                    sum += (x+1)*k;
                }
            }else{
                sum += k;
            }
        }
        System.out.println(sum);
    }
}

https://blog.csdn.net/weixin_52908342/article/details/135398934

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值