谈判(贪心算法)

题目

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();
	}
}
相关推荐
Arva .10 分钟前
讲一下 Spring 中用到的设计模式
java·spring·设计模式
bbq粉刷匠19 分钟前
Java-顺序表
java
dhdjjsjs23 分钟前
Day30 Python Study
开发语言·前端·python
十五年专注C++开发24 分钟前
async_simple:一个轻量级C++异步协程框架
开发语言·网络·c++·boost·asio
Tan_Ying_Y1 小时前
Mybatis的mapper文件中#和$的区别
java·tomcat·mybatis
难以触及的高度1 小时前
Java for循环完全指南:从基础到高性能实践
java·开发语言
wadesir1 小时前
用Python实现ggplot2风格绘图(零基础入门Seaborn与Matplotlib美化技巧)
开发语言·python·matplotlib
sheji34161 小时前
【开题答辩全过程】以 农产品销售系统为例,包含答辩的问题和答案
java·eclipse
油炸自行车1 小时前
【Qt】Qt Creator Debug模式提示“缺少 Windows CDB 调试器配套的扩展组件“”
开发语言·windows·qt
budingxiaomoli1 小时前
多线程(三)
java·开发语言