zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    Python生成器
    Python迭代器
    模块
    列表推倒式
    内置函数 lambda表达式
    函数
    global、nonlocal、迭代器
    练习
    编码数组字典
    字典
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947067.html
Copyright © 2011-2022 走看看