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.
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
思路:质数判断
#include
#include
using namespace std;
bool is_prime(int x)
{
if (x < 2) return false;
for (int i = 2; i <= x / i; i ++ )
if (x % i == 0)
return false;
return true;
}
int main()
{
int n,k,mod=1;
char s[1010];
scanf("%d %d",&n,&k);
scanf("%s",s);
if(n<k)
{
printf("404\n");
return 0;
}
for(int i=1;i<k;i++) mod*=10;
int num=0;
for(int i=0;i<k;i++) num=(num*10)+(s[i]-'0');
if(is_prime(num))
{
printf("%d\n",num);
return 0;
}
int pos=k;
while(pos<n)
{
num=(num%mod)*10+(s[pos]-'0');
if(is_prime(num))
{
for(int i=pos-k+1;i<=pos;i++) printf("%c",s[i]);
printf("\n");
return 0;
}
pos++;//千万别忘了!!
}
printf("404\n");
return 0;
}
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 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.
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
思路:模拟,主要是时间限制较紧张。type3要用unordered_map。
以及最最最重要的是输入输出不能用cin,cout;要用scanf,printf!!!
#include
#include
#include
#include
#include
using namespace std;
typedef pair<string,int> P;
P card[10010];
int n,m;
bool cmp(const P &a,const P &b)
{
return a.second==b.second? a.first<b.first : a.second>b.second;
}
void solve1()
{
char ch;
cin>>ch;
printf(" %c\n",ch);
vector<P> v;
for(int i=0;i<n;i++)
if(ch==card[i].first[0]) v.push_back(card[i]);
if(v.empty())
{
printf("NA\n");
return;
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<v.size();i++) printf("%s %d\n",v[i].first.c_str(),v[i].second);
}
void solve2()
{
string s;
cin>>s;
printf(" %s\n",s.c_str());
int cnt=0,score=0;
for(int i=0;i<n;i++)
if(s==card[i].first.substr(1,3))
{
cnt++;
score+=card[i].second;
}
if(cnt==0)
{
printf("NA\n");
return;
}
printf("%d %d\n",cnt,score);
}
void solve3()
{
string date;
cin>>date;
printf(" %s\n",date.c_str());
unordered_map<string,int>ans;
for(int i=0;i<n;i++)
if(date==card[i].first.substr(4,6)) ans[card[i].first.substr(1,3)]++;
vector<P>v(ans.begin(),ans.end());
if(v.empty())
{
printf("NA\n");
return;
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<v.size();i++) printf("%s %d\n",v[i].first.c_str(),v[i].second);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++) cin>>card[i].first>>card[i].second;
for(int i=1,type;i<=m;i++)
{
scanf("%d",&type);
printf("Case %d: %d",i,type);
if(type==1) solve1();
else if(type==2) solve2();
else solve3();
}
return 0;
}
1154 Vertex Coloring (25分)
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.
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
思路:模拟,用set的size表示颜色数目
#include
#include
using namespace std;
const int N=10010;
typedef pair<int,int>P;
P edge[N];
int color[N],n,m,k;
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++) scanf("%d %d",&edge[i].first,&edge[i].second);
scanf("%d",&k);
while(k--)
{
set<int>s;
for(int i=0;i<n;i++)
{
scanf("%d",&color[i]);
s.insert(color[i]);
}
bool success=true;
for(int i=0;i<m;i++)
{
if(color[edge[i].first]==color[edge[i].second])
{
printf("No\n");
success=false;
break;
}
}
if(success) printf("%d-coloring\n",s.size());
}
return 0;
}
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: Output Specification: 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: Sample Output 1: Sample Input 2: Sample Output 2: Sample Input 3: Sample Output 3: 思路:dfs输出路径,最后判断每一条路径是否为递增顺序。将节点value预处理,若可能为小根堆,则将所有数乘以-1(temp),最终只用判断是否为大根堆。不过不要忘记最后输出要乘以temp(啊啊啊,栽在这了!!!) 要注意的地方已在代码标注
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.
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#include