文章

8

粉丝

0

获赞

37

访问

833

头像
哈夫曼树 题解:C ,利用哈夫曼树 WPL 等于所有非叶子节点权值之和
P1382 北京邮电大学/兰州大学机试
发布于2026年3月14日 18:15
阅读数 205

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp (const void *a, const void *b) {
	int x = *(int *)a;
	int y = *(int *)b;
	return (x > y) - (x < y);
}

int main () {
	int N;
	while (scanf("%d", &N) == 1) {
		int arr[N];
		for (int i = 0; i < N; i++) {
			scanf("%d", &arr[i]);
		}
		
		int cnt = 0;
		int res = 0;
		while (cnt < N - 1) {
			qsort(arr + cnt, N - cnt, sizeof(int), cmp);
			arr[cnt + 1] = arr[cnt] + arr[cnt + 1];
			res += arr[cnt + 1];
			cnt++;
		}
		printf("%d\n", res);
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发