我因为一些事情没有参加2018年12月场的考试。2019年3月场的也没有报名,因为最近要开始去实习了,打算今年9月秋招的时候考一次吧。自己拿了三小时模拟考了一次。总体不是很难,主要是一些小细节,前三题各失小分,最后一题拿满。总共91分。。。
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.
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.
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.
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.
20 5
23654987725541023819
49877
10 3
2468024680
404
主要是判断字符串中指定位数的第一个素数。这题用substr() 和 sscanf() 即可轻松解决,不会用的自行百度一下。另外注意一个点,不足指定位数的素数,前面要用0填满。这里用了printf("%.*d",k,num)的方式来解决.
#include
#include
#include
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<
A registration card number of PAT consists of 4 parts:
T
for the top level, A
for advance and B
for basic;yymmdd
;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.
Each input file contains one test case. For each case, the first line gives two positive integers N (≤104) 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 Term
will 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.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:
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);Nt Ns
where Nt
is the total number of testees and Ns
is their total score;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
.
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
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
#include
#include
#include
#include
using namespace std;
bool cmp(const pair &a,const pair &b){
return a.second==b.second?a.firstb.second;
}
int main(){
int n,m;
cin>>n>>m;
string card;
int score;
vector> vec(n);
for(int i=0;i>card>>score;
vec[i] = pair{card,score};
}
for(int i=0;i>type>>term;
if(type == 1){
vector> temp;
for(int j=0;j mp;
for(int j=0;j> temp;
for(auto it:mp)
temp.push_back(pair{it.first,it.second});
printf("Case %d: %d %s\n",i+1,type,term.c_str());
if(temp.size() == 0){
cout<<"NA"<
A 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.
Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 104), 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.
For each coloring, print in a line k-coloring
if it is a proper k
-coloring for some positive k
, or No
if not.
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
4-coloring
No
6-coloring
No
水题,注意颜色的存储范围是int。如果用数组来帮助记录颜色总数时,必须开得太大,容易出现段错误。这里用set来帮助记录颜色总数。
#include
#include
#include
using namespace std;
vector> e;
int main(){
int n,m,k,a,b;
cin>>n>>m;
e.resize(n);
for(int i=0;i>a>>b;
e[a].push_back(b);
}
cin>>k;
for(int i=0;i color(n);
set s;
for(int j=0;j>color[j];
s.insert(color[j]);
}
int flag = 0;
for(int i=0;i
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.
Each input file contains one test case. For each case, the first line gives a positive integer N (1
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.
8
98 72 86 60 65 12 23 50
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
8
8 38 25 58 52 82 70 60
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
8
10 28 15 12 34 9 8 56
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
通过模拟dfs,以及用vector记录路径即可。因为是完全二叉树,当下标index大于结点总数时,即停止递归。注意pop_back()回朔路径即可。还有只有在叶子结点时才会打印路径,注意打印路径的条件。
#include
#include
#include
using namespace std;
vector vec,path;
vector> 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>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[i][j+1]){
flag = 1;
break;
}
if(flag == 1)
break;
}
if(flag == 0){
cout<<"Min Heap"<