huffman

package huffman;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		 Scanner scan = new Scanner(System.in);
		 int count = scan.nextInt();
		 int[] huf = new int[count];
		 for (int i = 0; i < count; i++) {
		 huf[i] = scan.nextInt();
		 }

//		int[] huf = {5, 3, 8, 2, 9};
		int len = huf.length;
		half(huf, len);
		int plus =0;
		int min1 = 0;
		int minCount = 0;
		for (int i = 1; i < len; i++) {
			min1 = huf[minCount] +huf[minCount+1];
			plus += min1;
			bubbo(huf,min1,minCount + 2);
			minCount++;
		}
		plus += huf[len-1];
		System.out.println(plus);
	}

	//冒泡移位
	public static void bubbo(int[] buf,int val,int index) {
		for(int i = index ;i= index + 1; j--)
					huf[j] = huf[j - 1];
				huf[index] = tmp;
				index = index + 1;
			}
		}
		half(huf, index);
		half(huf, len - index - 1);
	}
}

你可能感兴趣的:(huffman)