Java实现 洛谷 P1090 合并果子

在这里插入图片描述

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	private int []array;
	private int n, result;
    Main() {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        result = 0;
        n = Integer.parseInt(in.next());
        array = new int [n];
        for(int i=0; i<n; i++) {
        	array[i] = Integer.parseInt(in.next());
        }
        in.close();
    }
    private void print() {
		// TODO Auto-generated method stub
    	System.out.println(result);
	}
    private void run() {
    	int pos, temp;
    	Arrays.sort(array);
    	for(int i=1; i<n; i++) {
    		temp = array[i] + array[i-1];
    		result += temp;
    		
    		pos = i+1;
    		while(pos < n && array[pos] < temp) {
    			array[pos-1] = array[pos];
    			pos ++;
    		}
    		array[pos-1] = temp;
    	}
    	print();
    }
    public static void main(String[] args) {
        new Main().run();
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/13076677.html