Java 版本: public class T1Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { // 封装值 String str = scanner.nextLine(); Integer total = scanner.nextInt(); Set<Integer> ws = new TreeSet<>(); for (String item : str.split(",")) { ws.add(Integer.parseInt(item)); } // 简单贪心算法 Integer num = 0; Integer c = 0; for (Integer w : ws) { c += w; if (c > total) { break; } num++; } System.out.println(num); } } }
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务