PAT甲级 2018年12月场

我因为一些事情没有参加2018年12月场的考试。2019年3月场的也没有报名,因为最近要开始去实习了,打算今年9月秋招的时候考一次吧。自己拿了三小时模拟考了一次。总体不是很难,主要是一些小细节,前三题各失小分,最后一题拿满。总共91分。。。

1152 Google Recruitment (20 分)

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural constant e. The person who could find this prime number could go to the next step in Google's hiring process by visiting this website.

prime.jpg

The natural constant e is a well known transcendental number(超越数). The first several digits are: e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921... where the 10 digits in bold are the answer to Google's question.

Now you are asked to solve a more general problem: find the first K-digit prime in consecutive digits of any given L-digit number.

Input Specification:

Each input file contains one test case. Each case first gives in a line two positive integers: L (≤ 1,000) and K (< 10), which are the numbers of digits of the given number and the prime to be found, respectively. Then the L-digit number N is given in the next line.

Output Specification:

For each test case, print in a line the first K-digit prime in consecutive digits of N. If such a number does not exist, output 404 instead. Note: the leading zeroes must also be counted as part of the K digits. For example, to find the 4-digit prime in 200236, 0023 is a solution. However the first digit 2 must not be treated as a solution 0002 since the leading zeroes are not in the original number.

Sample Input 1:

20 5
23654987725541023819

Sample Output 1:

49877

Sample Input 2:

10 3
2468024680

Sample Output 2:

404

主要是判断字符串中指定位数的第一个素数。这题用substr() 和 sscanf() 即可轻松解决,不会用的自行百度一下。另外注意一个点,不足指定位数的素数,前面要用0填满。这里用了printf("%.*d",k,num)的方式来解决.

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

bool isprime(int num){
	if(num < 2)
		return false;
	for(int i=2;i<=sqrt(num);i++){
		if(num%i == 0)
			return false;
	}
	return true;
}

int main(){
	int l,k;
	cin>>l>>k;
	string str;
	cin>>str;
	for(int i=0;i<=l-k;i++){
		int num; 
		sscanf((str.substr(i,k)).c_str(),"%d",&num);
		
		if(isprime(num)){
			printf("%.*d\n",k,num);
			return 0;
		}	
	}
	cout<<404<<endl;
} 

 

1153 Decode Registration Card of PAT (25 分)

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
  • the 2nd - 4th digits are the test site number, ranged from 101 to 999;
  • the 5th - 10th digits give the test date, in the form of yymmdd;
  • finally the 11th - 13th digits are the testee's number, ranged from 000 to 999.

Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤10​4​​) and M (≤100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner's score (integer in [0,100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

  • Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Termwill be the letter which specifies the level;
  • Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
  • Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.

Output Specification:

For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

  • for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
  • for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
  • for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt's, or in increasing order of site numbers if there is a tie of Nt.

If the result of a query is empty, simply print NA.

Sample Input:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

简单的查询题,掌握好substr来提取子字符串即可。可用c_str()将string转为char输出。这里因为卡时间,用printf来输出,第三个查询用unordered_map(用map会超时,unordered_map查询快)。

#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

bool cmp(const pair<string,int> &a,const pair<string,int> &b){
	return a.second==b.second?a.first<b.first:a.second>b.second;
}

int main(){
	int n,m;
	cin>>n>>m;
	string card;
	int score;
	vector<pair<string,int>> vec(n);
	for(int i=0;i<n;i++){
		cin>>card>>score;
		vec[i] = pair<string,int>{card,score};
	}
	for(int i=0;i<m;i++){
		int type;
		string term;
		cin>>type>>term;
		if(type == 1){
			vector<pair<string,int>> temp;
			for(int j=0;j<n;j++){
				if((vec[j].first)[0] == term[0])
					temp.push_back(vec[j]);
			}
			printf("Case %d: %d %s\n",i+1,type,term.c_str());
			if(temp.size() == 0){
				cout<<"NA"<<endl;
				continue;
			}	
			sort(temp.begin(),temp.end(),cmp);
			for(int j=0;j<temp.size();j++)
				printf("%s %d\n",temp[j].first.c_str(),temp[j].second);
		}
		else if(type == 2){
			int cnt = 0,res = 0;
			for(int j=0;j<n;j++){
				if((vec[j].first).substr(1,3) == term){
					cnt++;
					res += (vec[j].second);
				}	
			}
			printf("Case %d: %d %s\n",i+1,type,term.c_str());
			if(cnt == 0){	
				cout<<"NA"<<endl;
				continue;
			}	
			cout<<cnt<<" "<<res<<endl;
		}
		else if(type == 3){
			unordered_map<string,int> mp;
			for(int j=0;j<n;j++){
				if((vec[j].first).substr(4,6) == term){
					mp[(vec[j].first).substr(1,3)]++;
				}
			}
			vector<pair<string,int>> temp;
			for(auto it:mp)
				temp.push_back(pair<string,int>{it.first,it.second});
			
			printf("Case %d: %d %s\n",i+1,type,term.c_str());
			if(temp.size() == 0){
				cout<<"NA"<<endl;
				continue;
			}	
			sort(temp.begin(),temp.end(),cmp);
			for(int j=0;j<temp.size();j++)
				printf("%s %d\n",temp[j].first.c_str(),temp[j].second);	
		}
	}
} 

1154 Vertex Coloring (25 分)

proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at most k colors is called a (proper) k-coloring.

Now you are supposed to tell if a given coloring is a proper k-coloring.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 10​4​​), being the total numbers of vertices and edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N−1) of the two ends of the edge.

After the graph, a positive integer K (≤ 100) is given, which is the number of colorings you are supposed to check. Then K lines follow, each contains N colors which are represented by non-negative integers in the range of int. The i-th color is the color of the i-th vertex.

Output Specification:

For each coloring, print in a line k-coloring if it is a proper k-coloring for some positive k, or No if not.

Sample Input:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

Sample Output:

4-coloring
No
6-coloring
No

水题,注意颜色的存储范围是int。如果用数组来帮助记录颜色总数时,必须开得太大,容易出现段错误。这里用set来帮助记录颜色总数。

#include <iostream>
#include <vector>
#include <set>
using namespace std;

vector<vector<int>> e;

int main(){
	int n,m,k,a,b;
	cin>>n>>m;
	e.resize(n);
	for(int i=0;i<m;i++){
		cin>>a>>b;
		e[a].push_back(b);
	}
	cin>>k;
	for(int i=0;i<k;i++){
		vector<int> color(n);
		set<int> s;
		for(int j=0;j<n;j++){
			cin>>color[j];
			s.insert(color[j]);
		}
		int flag = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<e[i].size();j++){
				if(color[i] == color[e[i][j]]){
					cout<<"No"<<endl;
					flag = 1;
					break;
				}	
			}
			if(flag == 1)
				break;	
		}	
		if(flag == 0)
			cout<<s.size()<<"-coloring"<<endl;
	}
} 

 

1155 Heap Paths (30 分)

In computer science, a heap is a specialized tree-based data structure 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 a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:

For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

Finally 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.

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap

通过模拟dfs,以及用vector记录路径即可。因为是完全二叉树,当下标index大于结点总数时,即停止递归。注意pop_back()回朔路径即可。还有只有在叶子结点时才会打印路径,注意打印路径的条件。

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

vector<int> vec,path;
vector<vector<int>> allpath;
int n,flag = 0;

void dfs(int index){
	if(index > n)
		return ;	
	path.push_back(vec[index]);
	dfs((index<<1)+1);
	if((index*2+1)>n && (index*2)>n){
		allpath.push_back(path);
		for(int i=0;i<path.size();i++)
			printf("%d%s",path[i],i==path.size()-1?"":" ");
		printf("\n");
	}
	dfs(index<<1);
	path.pop_back();
}

int main(){
	cin>>n;
	vec.resize(n+1);
	for(int i=1;i<=n;i++)
		cin>>vec[i];
	dfs(1);
	
	int flag = 0;
	for(int i=0;i<allpath.size();i++){
		for(int j=0;j<allpath[i].size()-1;j++)
			if(allpath[i][j] < allpath[i][j+1]){
				flag = 1;
				break;
			}
		if(flag == 1)
			break;	
	}
	if(flag == 0){
		cout<<"Max Heap"<<endl;
		return 0;
	}
		
	flag = 0;
	for(int i=0;i<allpath.size();i++){
		for(int j=0;j<allpath[i].size()-1;j++)
			if(allpath[i][j] > allpath[i][j+1]){
				flag = 1;
				break;
			}
		if(flag == 1)
			break;	
	}
	if(flag == 0){
		cout<<"Min Heap"<<endl;
		return 0;
	}
	cout<<"Not Heap"<<endl;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值