这次的作业完全是依葫芦画瓢,参照云课堂《数据结构》(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师课件中有关建堆及插入的内容,再加上自己写的一个矬函数(竟然传了4个参数),OK了!题设要求及代码实现如下

 /*
Name:
Copyright:
Author:
Date: 05/04/15 19:34
Description:
Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root. Input Specification: Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given. Output Specification: For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line. Sample Input:
5 3
46 23 26 24 10
5 4 3
Sample Output:
24 23 10
46 23 10
26 10
*/ #include <stdio.h>
#include <stdlib.h> #define MinData -10001 typedef struct HeapStruct
{
int * Elements;
int Size;
int Capacity;
} * MinHeap; MinHeap Create(int MaxSize);
void Insert(MinHeap H, int item);
void Print(MinHeap H, int * a, int N, int M); int main()
{
// freopen("in.txt", "r", stdin); // for test
int N, M, i, item;
MinHeap H; scanf("%d%d", &N, &M); H = Create(N);
for(i = ; i < N; i++)
{
scanf("%d", &item);
Insert(H, item);
} int a[M]; for(i = ; i < M; i++)
scanf("%d", &a[i]); Print(H, a, N, M);
// fclose(stdin); // for test
return ;
} MinHeap Create(int MaxSize)
{
MinHeap H = (MinHeap)malloc(sizeof(struct HeapStruct));
H->Elements = (int *)malloc((MaxSize + ) * sizeof(int));
H->Size = ;
H->Capacity = MaxSize;
H->Elements[] = MinData; return H;
} void Insert(MinHeap H, int item)
{
int i; i = ++H->Size;
for(; H->Elements[i / ] > item; i /= )
H->Elements[i] = H->Elements[i / ];
H->Elements[i] = item;
} void Print(MinHeap H, int * a, int N, int M)
{
int i; for(i = ; i < M; i++)
{
if(a[i] <= N)
{
while(a[i])
{
printf("%d", H->Elements[a[i]]);
if(a[i] > )
printf(" ");
else
printf("\n");
a[i] /= ;
}
}
}
}

PAT 05-树6 Path in a Heap的更多相关文章

  1. 05-树6. Path in a Heap (25) 小根堆

    05-树6. Path in a Heap (25) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...

  2. pat04-树9. Path in a Heap (25)

    04-树9. Path in a Heap (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Insert ...

  3. PAT005 Path in a Heap

    题目: Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index ...

  4. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  5. PAT甲级 树 相关题_C++题解

    树 目录 <算法笔记>重点摘要 1004 Counting Leaves (30) 1053 Path of Equal Weight (30) 1079 Total Sales of S ...

  6. PAT 03-树1 树的同构 (25分)

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后 ...

  7. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  8. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

随机推荐

  1. IoC 之 2.1 IoC基础(壹)

    2.1.1  IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器 ...

  2. Mybatis学习(贰)

    一.类型别名typeAlias 1.在mapper文件中:实体作为resultType,多次书写在配置文件中,每次需要书写权限名(com.baizhi.yanxj.entity.User),代码比较繁 ...

  3. DispatcherServlet中使用的特殊的Bean

    DispatcherServlet默认使用WebApplicationContext作为上下文,因此我们来看一下该上下文中有哪些特殊的Bean: 1.Controller:处理器/页面控制器,做的是M ...

  4. 带不带protype的区别

    总结写在前面: ①:带有protype:表示类的扩展,必须new后才能使用. ②:不带protype:属于静态方法,直接调用即可. html代码: <!DOCTYPE html> < ...

  5. 网络编程之socket(转)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...

  6. 10大html5前端框架

    Bootstrap 首先说 Bootstrap,估计你也猜到会先说或者一定会有这个( 呵呵了 ),这是说明它的强大之处,拥有框架一壁江山的势气.自己刚入道的时候本着代码任何一个字母都得自己敲出来挡我者 ...

  7. 今天遇到一个问题,linq语句的写法,查询动态字段

      public List<Location> getLocationList(int companyid, string searchValue, string searchField) ...

  8. c#中的反射

    System.Reflection.AssemblySystem.Activator Assembly assembly = Assembly.load("namespace名") ...

  9. iOS App Icon图标 尺寸规范

    Commit to AppStore:1024*1024 //for App IconIcon-60@3x.png:180*180 //iPhone 6 Plus (@3x)Icon-60@2x.pn ...

  10. MongoDb系列

    这个系列主要总结学习MongoDb过程中的一些经验. 简单介绍及环境搭建 常用命令 C#驱动及应用 管理工具MongoVUE使用