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.
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.
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!.
20190523
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
我的代码:
#include
using namespace std;
bool isprime(int x) {
if (x < 2) return false;
for (int i = 2; i*i <= x; i++) {
if (x%i == 0) return false;
}
return true;
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("Text.txt", "r", stdin);
#endif // DEBUG
string str;
cin >> str;
long long num;
int len = str.length();
int flag = 1;
for (int i = 0; i < len; i++) {
string s = str.substr(i, len - i);
printf("%s ", s.c_str());
num = stoi(s);
if (isprime(num)) {
printf("Yes\n");
}
else {
printf("No\n");
flag = 0;
}
}
if (flag == 1) {
printf("All Prime!");
}
}
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.
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.
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.
101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40
Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4
42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41
Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.
思路:两个unordered_set s,ans;s来存已经输入的元素,ans来存所有可以得到的差。
#include
#include
#include
using namespace std;
unordered_set<int> s,ans;
int A[12][1009];
int N, M;
bool visited[15];
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("Text.txt", "r", stdin);
#endif // DEBUG
int num1, num2,sum=0;
scanf("%d %d", &num1, &num2);
int diff = abs(num1-num2);//记录其差值
ans.insert(diff);
s.insert(num1); s.insert(num2);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf("%d", &A[i][j]);
}
}
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) {
if (visited[j] == true) continue;
//表示以及存在这个数字了,或者给出了错误的数字(不能由先前给出的两个数字求差得出)
if (s.find(A[j][i])!=s.end() || ans.find(A[j][i])==ans.end()) {
visited[j] = true;//踢出去
printf("Round #%d: %d is out.\n",i,j);
sum++;//记录胜利者,若为0就表示没有胜利者
continue;
}
s.insert(A[j][i]);
//每进来一个元素跟新s和ans;
for (auto it = s.begin(); it != s.end(); it++) {
ans.insert(abs((*it) - A[j][i]));
}
}
}
if (sum == N) {
printf("No winner.");
return 0;
}
else {
printf("Winner(s):");
for (int i = 1; i <= N; i++) {
if (visited[i] == false) printf(" %d", i);
}
}
}
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.
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 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. 思路:领接表存储这个图,然后判断相邻节点之间有没有相同的动物 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. 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 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. 模拟 Replace Selection Sort 的过程,就是说内存不够大只能一次排几个数balabala 总场考试下来就90来分,100多名好像,我太废物了太废物了,第二题 different 和 difference 傻傻分不清,浪费了1个小时。 一直在想not be duplicated 和 different 不是同一个意思吗,后来才发现是the difference of two nums previously…。 考完就开始一直写,一直写一直写,求个赞鼓励鼓励Output Specification:
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.
Input Sample
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
Output Sample
Yes
Error: Too many species.
Yes
No
Error: Too few species.
#include
7-4 Replacement Selection
Input Specification:
5
) and M (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
好长的英文题,好长好长,不过看看例子就懂了,输入N(数组的长度,输入的元素个数)。K表示一次最多可以排序的个数。
我模拟模拟这个过程:
一开始内存中肯定只有 81 94 11
三个数 排序后就是 11 81 94
然后和数组中的下一个数字进行比较,也就是99,进行比较发现99大于11,所以99和11一个梯队,并从内存中清除11,并将11加入第一梯队,并将99加进去……
过程写的有点着急,字有点丑,大家凑合凑合。
思路:有用优先队列的,有用vector的,有用set的。不过vector+sort肯定会超时,当然也可以对sort优化,这道题我最后一个测试点没过55555。留下菜比的眼泪,最好还是用自带排序的容器吧,避免超时。我就是个渣渣,肯定有大佬的思路比我好的多的。
大概就是用两个set来存放当前内存的第一梯队元素和第二梯队元素,然后第一梯队元素输出完了之后就令第二梯队为新的第一梯队,再将第二梯队clear()又能开始存储新的第二梯队元素了。我的渣渣思路,大家就当笑话看吧,最后一个测试点没过。评论中大哥给出的解
#include