7-1 Prime Day (20分)
The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Prime Day". That is, not only that the corresponding number of the date 20190523
is a prime, but all its sub-strings ended at the last digit 3
are prime numbers.
Now your job is to tell if a given date is a Prime Day.
Input Specification:
Each input file contains one test case. For each case, a date between January 1st, 0001 and December 31st, 9999 is given, in the format yyyymmdd
.
Output Specification:
For each given date, output in the decreasing order of the length of the substrings, each occupies a line. In each line, print the string first, followed by a space, then Yes
if it is a prime number, or No
if not. If this date is a Prime Day, print in the last line All Prime!
.
Sample Input 1:
20190523
Sample Output 1:
20190523 Yes
0190523 Yes
190523 Yes
90523 Yes
0523 Yes
523 Yes
23 Yes
3 Yes
All Prime!
Sample Input 2:
20191231
Sample Output 2:
20191231 Yes
0191231 Yes
191231 Yes
91231 No
1231 Yes
231 No
31 Yes
1 No
思路分析:
将给定数字以字符串的形式读取,然后依次求不同长度的子串,输出该子串,并将其转换为数字判断是否为素数,最后若全部是素数,则输出“All Prime!”。(注意,若转换为数字后输出将导致前导零无法输出从而答案错误,因此需要先以字符串形式输出再转换为数字)
参考代码:
#include
#include
#include
using namespace std;
bool is_prime(int a){
if(a <= 1) return false;
int sqr = (int)sqrt(1.0*a);
for(int i = 2; i <= sqr; i++){
if(a%i == 0) return false;
}
return true;
}
int main(){
string date, t;
int temp, flag = 1;
cin >> date;
for(int i = 0; i < date.length(); i++){
t = date.substr(i, date.length());
cout << t;
temp = stoi(t);
if(is_prime(temp)) printf(" Yes\n");
else{
flag = 0;
printf(" No\n");
}
}
if(flag) printf("All Prime!");
return 0;
}
7-2 The Judger (25分)
A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.
Your job is to write a judger program to judge the players' numbers and to determine the final winners.
Input Specification:
Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [1,10^5].
In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤10^3), the number of rounds.
Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (i=1,⋯,N). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.
Output Specification:
If the i
-th player is kicked out in the k
-th round, print in a line Round #k: i is out.
. The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 ... Wn
, where W1 ... Wn
are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner.
instead.
Sample Input 1:
101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40
Sample Output 1:
Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4
Sample Input 2:
42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41
Sample Output 2:
Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.
思路分析:
首先,理解清楚游戏规则是关键:1.所给数字不能与之前给出的数字重复。2.所给数字必须是之前所给数字的差。3.已经出局的玩家所给数字将不予考虑。然后,要注意数据必须先读入再模拟过程,因为游戏过程是先看列再看行(第一次没通过就是直接模拟过程了)。最后,可以这样来写:开一个set记录游戏过程中所给出的每一个数字(有序且不会重复),并设置一个变量来记录当前set中的最大元素值(边读入边更新),开一个数组记录某个数字之前是否已经存在,开一个vector记录出局玩家信息,开一个数组记录玩家是否出局。
游戏进行时,先看给数字的玩家是否已经出局,没有出局则读入一个数字。先看该数字之前是否出现过,若已经存在则该玩家直接出局并加入ans中。然后依次遍历set的每一个数字,并与该数字相加,查看和是否出现过,若出现过则该数字有效,记录下该数字并更新当前的最大数字;若和大于当前的最大数字,则该数字无效,加入ans中,并设置该玩家出局。
最后,输出失败玩家信息并看存储的出局玩家数是否等于总玩家数,相等则没有赢家,小于则遍历输出赢家id。(由于严格按照玩家给数字顺序模拟,答案顺序不用排列即符合要求)
参考代码:
#include
#include
#include
using namespace std;
struct node{
int round, id;
};
int N, M;
int check[15][1010];
bool exist[100010] = {false};
bool is_out[15] = {false};
set seq;
vector ans;
int main(){
int s1, s2, num, max_num; //记录目前集合中的最大元素,从而便于后续比较
scanf("%d %d", &s1, &s2);
max_num = max(s1, s2);
seq.insert(s1);
seq.insert(s2);
exist[s1] = exist[s2] = true;
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
scanf("%d", &check[i][j]);
}
}
for(int i = 1; i <= M; i++){//每一轮
for(int j = 1; j <= N; j++){//每个人
num = check[j][i];
if(!is_out[j]){
if(exist[num]){//这里不要忘记加入结果队列
is_out[j] = true;
ans.push_back(node{i, j});
}
else{
for(auto it = seq.begin(); it != seq.end(); it++){
if(exist[num + *it]){
seq.insert(num);
exist[num] = true;
max_num = max(max_num, num);
break;
}else if(num + *it > max_num){
is_out[j] = true;
ans.push_back(node{i, j});
break;
}
}
}
}
}
}
for(int i = 0; i < ans.size(); i++) printf("Round #%d: %d is out.\n", ans[i].round, ans[i].id);
if(ans.size() == N) printf("No winner.\n");
else{
printf("Winner(s):");
for(int i = 1; i <= N; i++){
if(!is_out[i]) printf(" %d", i);
}
}
return 0;
}
7-3 Safari Park (25分)
A safari park(野生动物园)has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.
Input Specification:
Each input file contains one test case. For each case, the first line gives 3 integers: N (0 Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space. Finally there is a positive M (≤20) followed by M lines of distribution plans. Each plan gives N indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations. For each plan, print in a line 思路分析: 参考代码: 7-4 Replacement Selection (30分) Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run. For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }. Your job is to implement this replacement selection algorithm. Each input file contains several test cases. The first line gives two positive integers N (≤10^5) and M ( For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All the numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line. 思路分析: 参考代码:Output Specification:
Yes
if no animals in the two neighboring regions are the same, or No
otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species.
or Error: Too few species.
according to the case.Sample Input:
6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
5
1 2 3 3 1 2
1 2 3 4 5 6
4 5 6 6 4 5
2 3 4 2 3 4
2 2 2 2 2 2
Sample Output:
Yes
Error: Too many species.
Yes
No
Error: Too few species.
本题是很常规的不用遍历图的信息题,即遍历所给的每一条边,看边的两个顶点是否相同。用set记录种类数。#include
When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.Input Specification:
Output Specification:
Sample Input:
13 3
81 94 11 96 12 99 17 35 28 58 41 75 15
Sample Output:
11 81 94 96 99
12 17 28 35 41 58 75
15
很遗憾,本题在规定时间内没有能够AC掉,主要原因在于没有真正理解外部排序的精髓也即没有理解透彻题意,一直在表面形式上打转,算法搞得很麻烦,也没有选择合适的数据结构来存储这些信息,最后肯定一团糟,不知道问题出在哪。其实,最重要的就是理解清楚题意,并选择最合适的数据结构来模拟过程,力求简洁明了。
使用一个向量数组来存储参与每一轮的数字序列,并采用两个循环:一个循环处理完当前轮次的所有数字,另一个循环处理所有轮次的向量。每次都对当前向量数字进行从大到小排序,并选择最小(也即末尾)数字输出,然后看给定序列后面还有没有数字,有的话,比较该数字和上述最小数字的大小,大于等于时加入本轮向量,否则加入下一轮的向量(也即只有大于等于才能在刚才输出数字后面输出)。最后向量全空时,外部排序结束。其中输出格式如何处理不再赘述。#include