谈判(贪心算法)

题目

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();
	}
}
相关推荐
皮皮林55120 小时前
告别 OOM:EasyExcel 百万数据导出最佳实践(附开箱即用增强工具类)
java
kylezhao201920 小时前
C# 语言基础(变量、数据类型、流程控制、面向对象编程)
开发语言·计算机视觉·c#·visionpro
咯哦哦哦哦20 小时前
WSL + ubantu22.04 + 远程桌面闪退+黑屏闪退解决
linux·开发语言
翩若惊鸿_20 小时前
【无标题】
开发语言·c#
Da Da 泓20 小时前
多线程(七)【线程池】
java·开发语言·线程池·多线程
To Be Clean Coder21 小时前
【Spring源码】getBean源码实战(三)
java·mysql·spring
杰瑞不懂代码21 小时前
基于 MATLAB 的 BPSK/QPSK/2DPSK 在 AWGN 信道下的 BER 性能仿真与对比分析
开发语言·matlab·qpsk·2psk·2dpsk
Wokoo721 小时前
开发者AI大模型学习与接入指南
java·人工智能·学习·架构
小鸡脚来咯21 小时前
python虚拟环境
开发语言·python
龘龍龙21 小时前
Python基础(九)
android·开发语言·python