谈判(贪心算法)

题目

java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		sc.nextLine();
		
    List<Integer> list = new ArrayList<>();
		for(int i=0;i<n;i++) {
			int x = sc.nextInt();
			list.add(x);
		}
		
    Collections.sort(list);//1 3 5 9
		
    int sum = 0;
		while(list.size()!=1) {
			int a = list.get(0);
			int b = list.get(1);
			int c = a+b;
			sum+=c;
			list.remove(0);//3 5 9
			list.remove(0);//5 9
			list.add(c);//5 9 4
			Collections.sort(list);//4 5 9
		}
		System.out.println(sum);
		sc.close();
	}
}
相关推荐
xieliyu.3 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
明夜之约3 小时前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee3 小时前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Jinkxs4 小时前
Resilience4j- 与 Spring Boot 快速集成:自动配置与基础注解使用
java·spring boot·后端
辣机小司4 小时前
【踩坑记录:Spring Boot 配置文件读取值不一致?警惕 YAML 的“八进制陷阱”与 SnakeYAML 版本之谜】
java·spring boot·后端·yaml·踩坑记录
CryptoPP4 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫4 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
fangdengfu1235 小时前
ES分析系统各个服务日志占用量
java·前端·elasticsearch
云烟成雨TD5 小时前
Spring AI 1.x 系列【51】可观测性技术选型
java·人工智能·spring
星越华夏5 小时前
ESP32-CAM图像传输项目说明文档
java·后端·struts·esp32