PAT甲级试水(1144,1145,1146)

 

144 The Missing Number (20)(20 分)

Given N integers, you are supposed to find the smallest positive integerthat is NOT in the given list.

Input Specification:

Each input file contains one test case. For each case, the first linegives a positive integer N (<= 10^5^). Then N integers are given inthe next line, separated by spaces. All the numbers are in the range ofint.

Output Specification:

Print in a line the smallest positive integer that is missing from theinput list.

Sample Input:

10
5 -25 9 6 1 3 4 2 5 17

Sample Output:

7

题目大意:给n个数,要求输出序列中缺失的最小正整数(1,3,4,5缺2)

 

思路:排序+遍历。做的时候考虑了全负数和一个正数的情况,没什么卵用,直接暴过。

 

代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
int p[100005];
int main()
{
	int t,i,a;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&a);
		if(a>0&&a<100005)
		{
		   p[a]=1;//此处若是用p[a]++,会出现段错误
	    }
	}
	for(i=1;i<=100015;i++)
	{
		if(p[i]==0)
		{
			printf("%d",i);
			break;
		}
	}
	return 0;
}

 

1145 Hashing - Average Search Time (25)(25 分)

The task of this problem is simple: insert a sequence of distinctpositive integers into a hash table first. Then try to find anothersequence of integer keys from the table and output the average searchtime (the number of comparisons made to find whether or not the key isin the table). The hash function is defined to be "H(key) = key % TSize"where TSize is the maximum size of the hash table. Quadratic probing(with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum sizegiven by the user is not prime, you must re-define the table size to bethe smallest prime number which is larger than the size given by theuser.

Input Specification:

Each input file contains one test case. For each case, the first linecontains two positive numbers: MSize, N, and M, which are theuser-defined table size, the number of input numbers, and the number ofkeys to be found, respectively. All the three numbers are no more than10^4^. Then N distinct positive integers are given in the next line. Allthe numbers in a line are separated by a space and are no more than10^5^.

Output Specification:

For each test case, in case it is impossible to insert some number,print in a line "X cannot be inserted." where X is the input number.Finally print in a line the average search time for all the M keys,accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

题目大意:给你ms,n,m,ms为哈希表长度,若ms不为素数,取大于ms最近的素数为哈希表长度(哈希表冲突处理要求使用二次探测),n个需要插入的数,若不能插入,输出“cannot be inserted”,m个需要查询的数,输出平均查询时间,精确到小数点后1位。

 

思路:真要在考场做就凉了,不知道二次探测是啥玩意儿,所以知识点覆盖还是差太多了,需要恶补。很直接的题,直接打哈希表操作即可。,,,气人的是,我的代码g++提交会错一个点显示的是浮点错误,懵的一匹。结果大佬告诉我用clang++提交就ok了,我...........二次探测讲解:blog.csdn.net/xyzbaihaiping/article/details/51607770

 

代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
using namespace std;
int p[100000]={0};
int  fun(int n)
{
	 if(n<=1) return 0;
	 int sqr=(int)sqrt(1.0*n);
	 for(int i=2;i<=sqr;i++)
	 {
	 	if(n%i==0)
	 	return 0;
	 }
	 return 1;
}
int main()
{
	int s,n,m,i,j,k,a;
	scanf("%d%d%d",&s,&n,&m);
	if(fun(s)==0)
	{
		k=s;
		while(1!=0)
		{
			k++;
			if(fun(k)==1)
			{
				break;
			}
		}
	}
	//cout<<k<<'*';
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		for(j=0;j<k;j++)
		{
			if(p[(a+j*j)%k]==0)
			{
				p[(a+j*j)%k]=a;
				break;
			}
		}
		if(j==k)
		{
			printf("%d cannot be inserted.\n",a);
		}
	}
	int b;
	double l=0.0;
	for(i=0;i<m;i++)
	{
		scanf("%d",&b);
		for(j=0;j<k;j++)
		{
			if(p[(b+j*j)%k]==0||p[(b+j*j)%k]==b)
			{
				break;
			}
		}
		l=l+j+1;
	}
	l=l/m;
	printf("%.1f",l);
	return 0;
}

 

1146 Topological Order (25)(25 分)

This is a problem given in the Graduate Entrance Exam in 2018: Which ofthe following is NOT a topological order obtained from the givendirected graph? Now you are supposed to write a program to test each ofthe options.

 

Input Specification:

Each input file contains one test case. For each case, the first linegives two positive integers N (<= 1,000), the number of vertices inthe graph, and M (<= 10,000), the number of directed edges. Then Mlines follow, each gives the start and the end vertices of an edge. Thevertices are numbered from 1 to N. After the graph, there is anotherpositive integer K (<= 100). Then K lines of query follow, each givesa permutation of all the vertices. All the numbers in a line areseparated by a space.

Output Specification:

Print in a line all the indices of queries which correspond to "NOT atopological order". The indices start from zero. All the numbers areseparated by a space, and there must no extra space at the beginning orthe end of the line. It is graranteed that there is at least one answer.

Sample Input:

6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output:

3 4

题目大意:给出有向图和拓扑顺序,判断拓扑顺序是否由给出有向图得出,输出满足条件的标号

 

思路:拓扑排序?又是知识漏洞,不熟悉,补补再回来。还是不怎么会,唉,菜。

 

代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<map>
#define Max 1005
using namespace std;
int n,m,k,limit[Max],q[Max],a,b,mp[Max][Max],ans[Max],ant;///limit存某个点前面有几个点先行
int fun()
{
    int p[Max];
    for(int i=1;i<=n;i++)///limit转到p
    {
        p[i]=limit[i];
    }
    for(int i=0;i<n;i++)
    {
        if(p[q[i]])///p为正表示顺序不合法
        {
            return 0;
        }
        for(int j=1;j<=n;j++)///如果合法 把受它限制的点 p都减1
        {
            if(mp[q[i]][j])p[j]--;
        }
    }
    return 1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&a,&b);
        mp[a][b]=1;
        limit[b]++;
    }
    scanf("%d",&k);
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&q[j]);
        }
        if(!fun())
        {
            ans[ant++]=i;
        }
    }
    for(int i=0;i<ant;i++)
    {
        if(i)printf(" %d",ans[i]);
        else printf("%d",ans[i]);
    }
return 0;
}

 

 

 

1147 Heaps (30)(30 分)

In computer science, a heap is a specialized tree-based datastructure that satisfies the heap property: if P is a parent node of C,then the key (the value) of P is either greater than or equal to (in amax heap) or less than or equal to (in a min heap) the key of C. Acommon implementation of a heap is the binary heap, in which the tree isa complete binary tree. (Quoted from Wikipedia athttps://en.wikipedia.org/wiki/Heap\_(data\_structure))

Your job is to tell if a given complete binary tree is a heap.

Input Specification:

Each input file contains one test case. For each case, the first linegives two positive integers: M (<= 100), the number of trees to betested; and N (1 < N <= 1000), the number of keys in each tree,respectively. Then M lines follow, each contains N distinct integer keys(all in the range of int), which gives the level order traversalsequence of a complete binary tree.

Output Specification:

For each given tree, print in a line "Max Heap" if it is a max heap, or"Min Heap" for a min heap, or "Not Heap" if it is not a heap at all.Then in the next line print the tree's postorder traversal sequence. Allthe numbers are separated by a space, and there must no extra space atthe beginning or the end of the line.

Sample Input:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56

Sample Output:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

题目大意:给出二叉树层序遍历,求判断是否为最小/最大堆。

 

 

 

 

思路:.....暂时不会,会了再回来补。(感觉欠了好多....)


试水总结:自不量力,开翻译补知识,试水失败成水鬼。淹死。知识点还是缺了很多,看来自学的数据结构还是太水,大二好好学一波。外加学好英语,争取明年过四级,争取明年能考甲级(小白又在妄想了)。

 
 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值