九度oj 题目1172:哈夫曼树
- 题目描述:
-
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
- 输入:
-
输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
- 输出:
-
输出权值。
- 样例输入:
-
5
1 2 2 5 9
- 样例输出:
-
37 权值为层数,从0开始,一开始本来想用数组来构造哈夫曼树,代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm> #define MAX 1009
#define HMAX 2009 struct Haff
{
int level;
int key;
bool isLeaf;
};
Haff haff[MAX]; int cmp(const void* a, const void *b) {
Haff at = *(Haff *)a;
Haff bt = *(Haff *)b;
if(at.key != bt.key)
return at.key - bt.key;
else if(at.isLeaf == bt.isLeaf) {
return ;
}
else {
if(at.isLeaf == true) {
return ;
}
else {
return -;
}
}
} int max(int a, int b) {
return a > b ? a: b;
}
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&haff[i].key);
haff[i].level = ;
haff[i].isLeaf = true;
}
int ptr = ;
int count = n;
int cen;
int N = * n - ;
while(ptr < N - ) {
qsort(&haff[ptr],count-ptr, sizeof(Haff), cmp); int pj = ptr + ;
cen = max(haff[ptr].level, haff[pj].level);
haff[ptr].level = cen;
haff[pj].level = cen;
haff[count].key = haff[ptr].key + haff[pj].key;
haff[count].level = cen + ;
haff[count].isLeaf = false;
count++;
ptr = ptr + ;
} int sum = ;
cen = haff[N-].level; /*for(int i = 0; i < count; i++) {
printf("%d %d\t",haff[i].key, haff[i].level);
}
printf("\n");*/
for(int i = ; i < count; i++) {
if(haff[i].isLeaf == true) {
sum = sum + (cen - haff[i].level) * haff[i].key;
}
}
printf("%d\n",sum);
}
return ;
}但这样做,level的值会出现错误。
后来发现去计算答案根本不需要建树,代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm> #define MAX 1009 int haff[MAX]; int cmp(const void* a, const void *b) {
int at = *(int *)a;
int bt = *(int *)b;
return at - bt; } int max(int a, int b) {
return a > b ? a: b;
}
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&haff[i]);
}
int ptr = ;
int count = n;
int N = * n - ;
int sum = ;
while(ptr < N - ) {
qsort(&haff[ptr],count-ptr, sizeof(int), cmp); int pj = ptr + ;
haff[count] = haff[ptr] + haff[pj];
sum = sum + haff[count];
count++;
ptr = ptr + ;
}
printf("%d\n",sum);
}
return ;
}
九度oj 题目1172:哈夫曼树的更多相关文章
- 九度oj 题目1088:剩下的树
题目描述: 有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
随机推荐
- vue2.0:(四)、首页入门,组件拆分1
为什么需要组件拆分呢?这样才能更符合模块化这样一个理念. 首先是index.html,代码如下: <!DOCTYPE html> <html> <head> < ...
- vs2013编译过程中,错误 59 error C4996: 'GetVersionExW': 被声明为已否决
好几次碰到这个错误,必须mark 一下!!!!!Project Properties > Configuration Properties > C/C++ > General > ...
- uvm_test——测试用例的起点
在UVM平台验证中,所有的test cases都extends uvm_test,首先,来看源代码 //------------------------------------------------ ...
- IOS开发之----详解在IOS后台执行
文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...
- Tunneling cannot be enabled without the local_ip bound to an interface on the host. Please configure local_ip 192.168.30.71 on the host interface to be used for tunneling and restart the agen
按照官方文档配置linux bridge 会出现一下问题 Tunneling cannot be enabled without the local_ip bound to an interface ...
- 使用Kubernetes里的job计算圆周率后2000位
使用Kubernetes里的job(作业),我们可以很方便地执行一些比较耗时的操作. 新建一个job.ymal文件: 定义了一个Kubernetes job,名称为pi,类型为job,容器名称为pi, ...
- UVA 12563 Jin Ge jin Qu [h] ao 劲歌金曲 (01背包)
每首只能唱一次,而且中间不能不唱歌,所以先把状态赋值为-1,以区别合法状态和非法状态,在唱歌曲目最多的条件下,离开时间应该尽量晚. 状态定义f[i][j]考虑前i首歌唱歌时间为j的最大唱歌曲目 #in ...
- 在web应用中使用日志
Log4J是Jakarta下的一个开源代码的子项目,用Log4J,我们可以使用定制的格式,把调试信息和日志信息输出到一个或多个需要的地方. 在Web应用中一般使用一个专门的Servlet来完成Log4 ...
- (五)使用Docker镜像(上)
1. 获取镜像 # 获取镜像 docker pull image:tag // 不使用tag 默认下载latest标签的镜像,即最新的镜像. 2. 查看镜像信息 # 查看镜像信息docker imag ...
- kitti raw data development kit的使用
run_demoVelodyne.m使用:http://blog.csdn.net/qq_33801763/article/details/78959205 https://www.cnblogs ...