谈判(贪心算法)

题目

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();
	}
}
相关推荐
WiChP1 天前
【V0.1B5】从零开始的2D游戏引擎开发之路
java·服务器·数据库
cch89181 天前
汇编与Java:底层与高层的编程对决
java·开发语言·汇编
荒川之神1 天前
拉链表概念与基本设计
java·开发语言·数据库
cch89181 天前
汇编与Go:底层到高层的编程差异
java·汇编·golang
chushiyunen1 天前
python中的@Property和@Setter
java·开发语言·python
禾小西1 天前
Java中使用正则表达式核心解析
java·python·正则表达式
yoyo_zzm1 天前
JAVA (Springboot) i18n国际化语言配置
java·spring boot·python
小樱花的樱花1 天前
C++ new和delete用法详解
linux·开发语言·c++
froginwe111 天前
C 运算符
开发语言