Solution to 2021 TIANTI competition

Posted by devans on Sat, 19 Feb 2022 00:34:08 +0100

2021 program design TIANTI competition was held on April 24. This article is part of the solution of TIANTI competition. Some problems did not get full marks at that time. Because the school opened the reproduction competition of TIANTI competition, write again.

Note: the answer in this article is not the standard answer. The score of each question is written in the corresponding position. Please check it.

Some quit and some joined. Be consistent and never forget your original heart.

L1-1 man and God (5 points / 5 points): the first 5 points is the score obtained by the code, and the second 5 points is the total score of the topic, the same below
L. Peter Deutsch has a famous saying: "to iterate is human, to recurse divide." (iterative is human, recursive is God). Please output this sentence directly on the screen.

Input format:

There is no input for this question.

Output format:
Output in one line

To iterate is human, to recurse divine.. 

Input sample:

nothing

Output example:

To iterate is human, to recurse divine.

[problem analysis] check in questions and output them directly.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
int main(){
    cout<<"To iterate is human, to recurse divine."<<endl;
    system("pause");
    return 0;
}
L1-2 finish learning C language in two hours (5 points / 5 points)

A baby asked, "how can I finish learning C language in two hours?" Of course, the question is "learning" is not "learning". Suppose a C language textbook has N words, and the baby can read K words per minute for M minutes. How many words left?

Input format

Input gives three positive integers in one line, which are N(Not more than 400000), the total number of words in textbooks; K(No more than 3000), which is the number of words the baby can read per minute; M(No more than 120), which is the number of minutes the baby reads.

The title ensures that the number of words read by the baby does not exceed N.

Output format:

Output the number of words the baby has not read in one line.

Input sample:

100000 1000 72

Output example:

28000

[problem analysis] don't be as impetuous as this baby. Simple arithmetic problems can be solved by n-k*m;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
    int n,m,k;
    cin>>n>>m>>k;
    cout<<n-m*k<<endl;
    return 0;
}
L1-3 obsessive compulsive disorder (10 points / 10 points)
Xiaoqiang is counting the date of birth of residents in a community, but he finds that the birthday format filled in by everyone is not unified. For example, some people write 199808, while others only write 9808. Xiaoqiang with obsessive-compulsive disorder, please write a program to sort out the date of birth of everyone in the format of year, year, month and month of adulthood. For those information that only write the last two digits of the year, we default to less than 22, which starts with 20, and others start with 19.

Input format:

Enter a date of birth in one line, which is a 6-digit or 4-digit number. The title is guaranteed to be the legal month and year between January 1000 and December 2021.

Output format:

Year after year in a row in standard format-Organize and output the input information every month.

Input example 1:

9808

Output example 1:

1998-08

Input example 2:

0510

Output example 2:

2005-10

Input example 3:

196711

Output example 3:

1967-11

[problem analysis] for this question, you can see that the input can be divided into two formats:

  • Complete spelling of the year, with a total of 6 characters in the string;
  • The year is not spelled completely, only the last two digits are written, and the string has a total of 4 digits; For this kind of string, according to the title description, we default that less than 22 starts with 20, and the others start with 19, which can be divided into two cases:
    • The year less than 22 is the year of the 21st century;
    • Others are 20th century years;

The test site of this question is a branch structure.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
    string s;
    cin>>s;
    if (s.size()==4){ //abbreviation
        int year = (s.at(0)-'0')*10+(s.at(1)-'0');    //Get the first two values
        if (year<22){
            cout<<20;
        } else{
            cout<<19;
        }
        cout<<s.at(0)<<s.at(1)<<"-"<<s.at(2)<<s.at(3);
    } else{ //detailed
        for (int i=0;i<s.size();i++){
            cout<<s.at(i);
            if (i==3){
                cout<<"-";
            }
        }
    }
    return 0;
}
L1-4 price reduction reminder robot (10 points / 10 points)
Small T wants to buy a toy for a long time, but the price is a little high. He plans to wait until it is cheaper. But it's troublesome to stare at the shopping website every day. Please help Xiao T write a price reduction reminder robot to send out a reminder when the current price of the toy is cheaper than the price he set.

Input format:

The first line of input is two positive integers N and M (1 ≤ N ≤ 100, 0 ≤ M ≤ 1000), indicating that there are N price records, and the price set by small T is M.

Next N lines, each line has a real number P**i (− 1000.0 < P**i < 1000.0), representing a price record.

Output format:

For each price record P cheaper than the set price M, output on sale in one line! P. Where p is output to 1 digit after the decimal point.

Input sample:

4 99
98.0
97.0
100.2
98.9

Output example:

On Sale! 98.0
On Sale! 97.0
On Sale! 98.9

[problem analysis] use the circular structure to compare each price record and find cheaper reminders;

The test point of this question is: circular structure

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
    int n;
    double m,p;
    cin>>n>>m;
    for (int i=0;i<n;i++){
        cin>>p;
        if (p<m){
            printf("On Sale! %.1lf\n",p);
        }
    }
    return 0;
}
L1-5 Big Ben's mood (15 minutes / 15 minutes)

Some netizens asked: will there be more big ben questions in the future? Benzhong replied: look at your mood... Please write a program for big benzhong and output the answer automatically according to your mood.

Input format:

Input an integer in 24 [0, 100] intervals in one line, which successively represents the mood index of Big Ben in each hour of 24 hours a day.

Then there are several lines. Each line gives an integer between [0, 23], representing the time point when the netizen asked stupid clock. When an illegal time point occurs, it means that the input is over. Do not process this illegal input. Ensure that the questions are asked at least once.

Output format:

For each question, if the mood index of stupid clock is greater than 50, output the mood index Yes in one line, otherwise output the mood index No.

Input sample:

80 75 60 50 20 20 20 20 55 62 66 51 42 33 47 58 67 52 41 20 35 49 50 63
17
7
3
15
-1

Output example:

52 Yes
20 No
50 No
58 Yes

[problem analysis] Big Ben is a frequent visitor of the ladder race. After three years ~, today I finally know that Big Ben was grandma's self proclaimed hhh. Return to the topic, which may be the simplest problem in Big Ben Series in recent years. We can use an array to save values, and then access and output the given query.

The test point of this question is: array

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main()
{
    int que, mood[24];
    for (int i = 0; i < 24; i++)
    {
        cin >> mood[i];
    }
    while (1)
    {
        cin >> que;
        if (que < 0 || que > 23)
        {
            break;
        }
        cout << mood[que] << " ";
        if (mood[que] > 50)
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    return 0;
}

L1-6 teacher Ji's return (15 points / 15 points)

Mr. Ji, who once killed everyone in the TIANTI competition, decided to return to the TIANTI competition! In order to simplify the topic, we might as well assume that each topic of TIANTI competition can be described by a string that does not exceed 500 and only includes printable symbols, such as Problem A: Print "Hello world!". As we all know, Mr. Ji's competition level is very excellent. You can think he can do every topic (in fact...). Therefore, Mr. Ji will look at the questions in order and do them. But Mr. Ji's level is too high, so he doesn't bother to do the check-in question (wasting time). Specifically, if there is qiandao or easy (case sensitive) in the string of the question, Mr. Ji won't do it after reading the question. Now, given the total number of questions in this ladder race and how many questions Mr. Ji has finished, please tell us which question Mr. Ji is doing now, or Mr. Ji has finished all the questions he intends to do.

Reminder: there are rules for score upgrading in TIANTI competition. If you don't do the check-in question, the total score of the team may not be enough to upgrade. Ordinary players please don't learn Mr. Ji's cool behavior!

Input format:

The first line of input is two positive integers N,M (1 ≤ M ≤ N ≤ 30), indicating that there are N questions in this ladder competition. Mr. Ji has finished M now.

The next N lines, each of which is a string that matches the title description, represent the title content of the TIANTI competition. Mr. Ji will look at the questions in the order given - the first line is the first question Mr. Ji looks at, the second line is the second question, and so on.

Output format:

Output the question plane corresponding to the question Mr. Ji is currently working on in one line (that is, which question Mr. Ji is working on after finishing M questions). If Mr. Ji has finished all the problems he intends to do, output a line Wo AK le.

Input example 1:

5 1
L1-1 is a qiandao problem.
L1-2 is so...easy.
L1-3 is Easy.
L1-4 is qianDao.
Wow, such L1-5, so easy.

Output example 1:

L1-4 is qianDao.

Input example 2:

5 4
L1-1 is a-qiandao problem.
L1-2 is so easy.
L1-3 is Easy.
L1-4 is qianDao.
Wow, such L1-5, so!!easy.

Output example 2:

Wo AK le

[problem analysis] the main idea of string processing is to give a value m to represent the completed topic, and if the words "qiandao" and "easy" appear in the topic, they will be ignored (pay attention to case sensitivity). Therefore, we only need to subtract one from m after passing through a string that does not contain the above two substrings, Until m equals 0 or AK (All Killed) all the questions (this sentence actually repeats the case where m equals 0 and AK, ignoring the details ~).

The test points of this question are: string processing;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main()
{
    int n,m;
    cin>>n>>m;
    string in;
    getchar();
    for (int i=0;i<n;i++){
        getline(cin,in);
        if (in.find("qiandao")!=in.npos||in.find("easy")!=in.npos){
            continue;
        }
        m--;
        if (m<0){
            cout<<in<<endl;
            return 0;
        }
    }
    cout<<"Wo AK le"<<endl;
    return 0;
}
L1-7 goodness of TIANTI race (20 points / 20 points)
The ladder race is a kind race. The kind proposition group hopes to control the difficulty of the topic within a range, so that each participating student has a topic that can be done, and the most powerful students have to work very hard to get high scores. So the propositional group first divided the programming ability into 106 grades (too crazy, which was false), and then investigated the programming ability of each participating student. Now please write a program to find out the minimum and maximum ability values of all participating students and give it to the proposition group as a reference for the problem.

Input format:

Input gives a positive integer N (≤ 2) in the first line × 104), that is, the total number of participating students. The next line gives N positive integers no more than 106, which are the ability values of the participating students.

Output format:

The first line outputs the minimum ability value of all participating students and the number of students with this ability value. The second line outputs the maximum ability value of all participating students and the number of students with this ability value. The numbers in the same line shall be separated by one space, and there shall be no extra space at the beginning and end of the line.

Input sample:

10
86 75 233 888 666 75 886 888 75 666

Output example:

75 3
888 2

[problem analysis] find the minimum and maximum values in the sequence and count them. The data scale is 10 ^ 4, so they can be sorted directly. For counting, my common methods include array counting (advantages: convenient and fast; disadvantages: it consumes more space, especially when there is more data) and structure counting (advantages: it saves space; disadvantages: it is inconvenient to write cmp parameters), Array count is used here.

The test points of this question are sorting * * and counter

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int Count[1000001];
int main()
{
    int n,s[20001];
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>s[i];
        Count[s[i]]++;
    }
    sort(s,s+n);
    cout<<s[0]<<" "<<Count[s[0]]<<endl;
    cout<<s[n-1]<<" "<<Count[s[n-1]]<<endl;
    return 0;
}
L1-8 multiplication formula sequence (20 points / 20 points)
This question requires you to start from any given two 1-digit numbers a1 and a2 and use the multiplication formula to generate a sequence {a**n}. The rule is to start from a1 in sequence, multiply the current number by the next number each time, and paste the result at the end of the sequence. If the result is not 1 digit, each digit shall become an item of the sequence.

Input format:

Input: three integers are given in one line, which are a1,a2 and N in sequence, meeting the requirements of 0 ≤ a1,a2 ≤ 9 and 0 < n ≤ 103.

Output format:

Output the first n items of the sequence in one row. The numbers shall be separated by one space, and there shall be no extra space at the beginning and end of the line.

Input sample:

2 3 10

Output example:

2 3 6 1 8 6 8 4 8 4

Example explanation:

The first two items of the sequence are 2 and 3. Start with 2 because 2 × 3 = 6, so item 3 is 6. Because 3 × 6 = 18, so items 4 and 5 are 1 and 8 respectively. And so on... Finally, because item 6 has 6 × 8 = 48, corresponding to items 10 and 11 should be 4 and 8. Since only the first 10 items are required to be output, it ends after output 4.

[problem analysis] this question needs to be read carefully. Now simplify the question:

  • Each element of the sequence is the product of the first two elements;
  • If the product of this time is 2 digits (it is impossible to have 3 or more digits, and the maximum is 9 * 9 = 81), disassemble the ten digits and one digit as the new two digits;
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int Count[1000001];
int main()
{
    int a1, a2, n;
    int content[1005], index = 3; //Index refers to the content pointer. When index==n, it needs to be output. The initial value is 2 because there are already a1 and a2
    int now = 2;                  //now is the second number ready for the operation

    cin >> a1 >> a2 >> n;
    content[1] = a1;
    content[2] = a2;

    if (n == 1)
    {
        cout << a1<<endl;
        return 0;
    }
    else if (n == 2)
    {
        cout << a1<<" "<<a2 << endl;
        return 0;
    }
    
    cout << a1 << " " << a2;
    stringstream ss;
    while (1)
    {
        ss.clear();
        ss.str("");
        int tmp = content[now] * content[now - 1];
        ss << tmp;
        if (ss.str().size() == 1)
        {
            content[index++] = tmp;
            cout << " " <<tmp;
            if (index - 1 == n)
            {
                return 0;
            }
        }
        else
        {
            content[index++] = ss.str().at(0)-'0';
            cout<<" "<<content[index-1];
            if (index - 1 == n)
            {
                system("pause");
                return 0;
            }
            content[index++] = ss.str().at(1)-'0';
            cout<<" "<<content[index-1];
            if (index - 1 == n)
            {
                return 0;
            }
        }
        now++;
    }
    return 0;
}

L2-1 packaging machine (25 points / 25 points)
The structure of an automatic packaging machine is shown in Figure 1. First, there are N tracks in the machine and some items are placed. There is a basket under the track. When the button of a track is pressed, the piston pushes to the left to push an item at the end of the track into the basket. When button 0 is pressed, the manipulator will grab an item at the top of the basket and put it on the assembly line. Figure 2 shows the state of the packaging machine after pressing buttons 3, 2, 3, 0, 1, 2 and 0 in sequence.

In a special case, because the capacity of the basket is limited, when the basket is full, but the button of a track is still pressed, the system shall forcibly start key 0, first grab an item from the basket, and then push the item of the corresponding track down. In addition, if the track is empty, nothing will happen by pressing the corresponding button again; Similarly, if the basket is empty, nothing will happen by pressing button 0.

Now a series of button operations are given. Please list the items on the assembly line in turn.

Input format:

Input the first line to give three positive integers N (≤ 100), M (≤ 1000) and Sma**x (≤ 100), which are the number of tracks (so the tracks are numbered from 1 to N), the number of items initially placed in each track, and the maximum capacity of the basket. Then N lines, each line gives M capital letters, indicating the initial placement of items in each track.

The last line gives a series of numbers, which correspond to the number of the pressed button in sequence until the − 1 sign is input. This number should not be processed. Numbers are separated by spaces. The title ensures that at least one item will be taken out and put on the assembly line.

Output format:

The items on the assembly line shall be output sequentially in one line without any spaces.

Input sample:

3 4 4
GPLT
PATA
OMSA
3 2 3 0 1 2 0 2 2 0 -1

Output example:

MATA

For a given pipeline, the problem of using the characteristics of [stack] and [basket] is to analyze the characteristics of a given pipeline respectively; It mainly investigates the ability of abstracting and applying data structure to solve problems from practice. Skilled use of STL can provide great convenience for solving this problem;

The following will simplify the topic:

  • For each track (queue), when pressing their corresponding track number, the elements of the team head will be out of the team and pop out of the basket (stack);
  • When pressing 0, put the elements at the top of the basket (stack) at the end of the pipeline (queue);
  • Two situations require special attention:
    • After the basket (stack) is full, the "0" operation needs to be forced to be triggered the next time the operation of Track - > basket is triggered, that is, the operation of basket - > pipeline;
    • When there is no goods in the basket, nothing will happen by pressing "0";
    • When there is no goods on the track, nothing will happen when you press to pick up the goods;

Test site: queue, stack, STL

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main()
{
    int n, m, Max;
    cin >> n >> m >> Max;
    vector<queue<char> > tracks(101); //track
    stack<char> backet;               //basket
    queue<char> sl;                   //streamline pipeline

    char k;
    for (int i = 1; i <= n; i++)
    {
        getchar();
        for (int j = 0; j < m; j++)
        {
            cin >> k;
            tracks[i].push(k);
        }
    }
    int op;
    while (1)
    {
        cin>>op;
        if (op==-1){
            break;
        }
        if (op==0){                         //Basket - > pipeline (stack - > queue)
            if(!backet.empty()){            //No reaction when the basket is empty
                sl.push(backet.top());
                backet.pop();
            }
        } else{                             //Track - > basket (queue - > stack)
            if (!tracks[op].empty()){       //The orbit is empty and does not react
                if (backet.size()==Max){    //The basket is full now
                    sl.push(backet.top());
                    backet.pop();
                    backet.push(tracks[op].front());
                    tracks[op].pop();
                } else{
                    backet.push(tracks[op].front());
                    tracks[op].pop();
                }
            }
        }
    }
    while (!sl.empty()){
        cout<<sl.front();
        sl.pop();
    }
    return 0;
}
L2-2 virus traceability (25 points)
Viruses are prone to mutation. A virus can produce several mutated strains through mutation, and these mutated viruses may be induced to mutate to produce the second generation mutation, so they continue to change. Given the variation relationship between some viruses, you are required to find the longest variation chain. It is assumed that all the mutations given here are caused by mutation, and the complex problem of gene recombination mutation is not considered - that is, each virus is mutated by a unique virus, and there is no circular mutation.

Input format:

The input gives a positive integer N (≤ 104) in the first line, that is, the total number of virus types. So we numbered all viruses from 0 to N − 1.

Then N lines, each describing the variation of a virus in the following format:

k Variant 1... Variant k

Where k is the number of variants produced by the virus, followed by the number of each variant. Line i corresponds to the virus numbered i (0 ≤ i < n). The problem is to ensure that there is and only one source of the virus.

Output format:

First, the length of the longest variation chain from the source is output.

In the second line, the longest variation chain from the source is output. The numbers are separated by one space, and there shall be no redundant spaces at the beginning and end of the line. If the longest chain is not unique, the minimum sequence is output.

Note: we call the sequence {a1,..., a**n} smaller than the sequence {b1,..., b**n}. If there is 1 ≤ K ≤ n, a**i=b**i is true for all I < K, and a * * k < B * * K.

Input sample:

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

Output example:

4
0 4 9 1

[problem analysis] when I first saw this problem, I thought it was necessary to use and search the set. In fact, this is a problem of finding the longest path in a tree,

Suddenly received the task of the computer competition, and then make up (dig a pit) after May Day.

Topics: Java data structure stack