# Brush notes (C + + implementation) pta005 continue 3n+1 conjecture

Posted by phpQuestioner_v5.0 on Wed, 22 Sep 2021 18:35:29 +0200

catalogue

Title Description and requirements:

Input format:

Output format:

Input example:

Output example:

Question making process:

Source code:

deficiencies:

Good night, people.

## Title Description and requirements:

1005 continue (3n+1) conjecture (25 points)

Callatz conjecture has been described in 1001. In this subject, the situation is a little complicated.

When we verify the karatz conjecture, in order to avoid repeated calculation, we can record every number encountered in the recursive process. For example, yes   n=3   For verification, we need to calculate 3, 5, 8, 4, 2 and 1   When n=5, 8, 4 and 2 are verified, the authenticity of karaz conjecture can be directly determined without repeated calculation, because these four numbers have been encountered when verifying 3. We call 5, 8, 4 and 2 "covered" by 3. We call a number in a sequence   n   Is "key number", if   n   Cannot be overwritten by other numbers in the sequence.

Now, given a series of numbers to be verified, we only need to verify several key numbers, so we don't have to verify the remaining numbers again. Your task is to find these key numbers and output them in descending order.

### Input format:

Each test input contains one test case, and the first line gives a positive integer   K   (< 100), given in line 2   K   Two different positive integers to be verified   n   (1 < n ≤ 100), numbers are separated by spaces.

### Output format:

The output of each test case occupies one line, and the key numbers are output in order from large to small. Numbers are separated by a space, but there is no space after the last number in a line.

```6
3 5 6 7 8 11```

`7 6`

## Question making process:

1. I just saw that this topic was misled by the information of the topic. In the topic, "you can record every number encountered in the recursive process" gives people the wrong misleading idea that this topic needs to use an array or container to store the numbers encountered in the recursive process, and then compare them with the initially entered numbers

2. The direction of priority analysis is wrong. Instead of focusing on what conditions are not covered, we are thinking about how to store data, which puts the cart before the horse (in fact, it is easy to store arrays and containers). At first, we thought that we should use two containers, one to store the numbers encountered in recursion and the other to store the numbers encountered at the beginning.

3. At first, the code was very cumbersome because it was not keen to find the conditions that were not covered. It was written that the code was repeatedly jammed and modified in the middle of the process. After careful analysis, it was found that in fact, each input number produced A sequence, and the coverage conditions were similar to that one of them was A sub sequence of the other. When you think about it, the problem was very clear, that is, the number one encountered in the recursion of B to 1 Once it is the same as the number originally entered (recorded as A), A is overwritten by B.

4. Later, it was found that there was no need to set up a special container to store the numbers encountered in recursion, which could be compared online immediately after each function call.

5. It is more important here. First of all, it is found that the input of the chopping function should be & A, not a.

The difference between the two: pass in & A to change the original value; pass in a to only operate on the copy without changing to the original value

Even if the function parameters are modified.

6. It is found that one container is not enough, because the elements in one container are changed after being called by the chopping function, and the subsequent numbers cannot check whether the previous numbers are overwritten (the previous numbers have become 1). Therefore, set two containers and add a new container (numbersr2) as the unchanged initial value copy to check the coverage.

7. Think of using an array with only zero and one state to mark whether the state is overwritten.

8. After marking, traverse the copy and add a container to output the number that has not been overwritten.

9. The answer is wrong. I'll go. It turns out that this topic is very boring. There are also requirements for the output format: 1. Output from large to small. 2. The last number can't have spaces. OK, I'll change it.

10. Learned the sort function sorting (sort default ascending sorting). Why don't you ask me why I don't write fast, row bubbles and so on? Sorry, the algorithm hasn't learned that yet.

11. Close.

## Source code:

```#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector< int> numbers1, numbers2, numbers3;/*numbers2 As copy
numbers3 Store numbers that are not overwritten for next sorting*/
int cover;//Records whether it is overwritten or not. The default initialization is 0 until it is global
//Set up a function to help us perform each counting operation
void cut( int &a)
{
if (a % 2)//If a is odd
a = (3 * a + 1) / 2;
else//If a is even
a /= 2;
}
int main()
{
int num = 0, a = 0;
cin >> num;
for ( int i = 0; i != num; ++i)//Read the corresponding number
{
cin >> a;
numbers1.push_back(a);//Put the number in container 1
numbers2.push_back(a);//Container 2 places a set of initial copies
}
for (int i = 0; i != num; ++i)//Traverse all numbers entered
{
while (numbers1[i] != 1)//Cut to one
{
cut(numbers1[i]);//Each time the function runs, the passed in value is updated to a procedure value
for (int j = 0; j != num; ++j)
{
if (numbers1[i] == numbers2[j])//If the number of procedure sub columns appears in the original number set
cover[j] = 1;//Equal to 1 means that the number is overwritten
}
}
}
for (int i = 0; i != num; ++i)
{
if (!cover[i])//If it's not covered
{
numbers3.push_back(numbers2[i]);//Put the uncovered number into container 3
}
}
sort(numbers3.begin(), numbers3.end());
for (int i = numbers3.size() - 1; i >= 1; --i)
cout << numbers3[i] << " ";
cout << numbers3;//Because there is no space after the last data, hit it once by hand;
return 0;
}```

## deficiencies:

1. It took too long to do the questions. At first, I didn't concentrate on thinking about the coverage conditions.

2. Check that the coverage part is nested for three times, which is very complex. Tomorrow, I'll be free to see what the leaders of CSDN do and learn from their experience.

3. I don't know how to sort by hand. Using functions makes me very low.

## Good night, people.

Topics: C++ Algorithm