Continue the (3n+1) conjecture

Continue the (3n+1) conjecture

Callatz conjectures:

For any positive integer n, if it is even, cut it in half; If it is odd, cut (3n+1) in half. This has been repeatedly cut down, and finally you must get n=1 at a certain step. Karaz announced this conjecture at the World Conference of mathematicians in 1950. It is said that at that time, the teachers and students of Yale University mobilized together to try their best to prove this seemingly silly and naive proposition. As a result, the students did not want to study and only proved (3n+1), so that some people said it was a conspiracy. Karaz was deliberately delaying the progress of teaching and scientific research in American Mathematics

When we verify the karatz conjecture, in order to avoid repeated calculation, we can record every number encountered in the recursive process. For example, when verifying n=3, we need to calculate 3, 5, 8, 4, 2 and 1. When verifying n=5, 8, 4 and 2, we can directly determine the authenticity of karaz conjecture 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 N in a sequence "key number", if n cannot be covered 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. The first line gives a positive integer k (< 100), and the second line gives the values of K different positive integers n (1 < n ≤ 100) to be verified. The 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.

Input example:

6
3 5 6 7 8 11

Output example:

7 6

C++(g + +) code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Get the number of overwritten
void get_nums(int, vector<int> &);
// Gets the result array obtained after each circular deletion
void get_result(vector<int>, vector<int> &);

int main() {
    // Number of k to be verified for receiving input
    vector<int> input_nums;
    // K for receiving input, i.e. k=count
    int count;
    // It is used to receive the number of each input for storage in input_nums
    int num;
    // Enter number count
    cin >> count;
    // Loop in count numbers to be verified
    for(int i = count; i > 0; -- i)
    {
        cin >> num;
        // Store the input number to be verified into input_nums
        input_nums.push_back(num);
    }
    // Since the number of verified input is > 1, the case of 1 does not need to be considered
    // Defines the array used to store the results
    vector<int> result_nums;
    // Initialization, data and input_ Same as nums
    result_nums.assign(input_nums.begin(), input_nums.end());
    // Traverse each number to be verified
    for(auto n : input_nums)
    {
        // Defines the array used to receive the number of overrides
        vector<int> nums;
        // Get the number of overwrites corresponding to n
        get_nums(n, nums);
        // Gets or deletes the array after the number to be verified contains the number overwritten by n
        get_result(nums, result_nums);
    }
    // Sort the result array from small to large
    sort(result_nums.begin(), result_nums.end());
    // Reverse the result array to obtain the result array sorted from large to small
    reverse(result_nums.begin(), result_nums.end());
    // Calculation result array length
    int len = result_nums.size();
    // Loop through the result array
    for(int i = 0; i < len; ++ i)
    {
        // If the number of passes is the first bit of the array, it is output directly
        if(i == 0)
        {
            cout << result_nums[i];
        }
        // Otherwise, if the traversed number is not the first, it will be preceded by a space
        else
        {
            cout << " " << result_nums[i];
        }
    }
    return 0;
}

// Gets the number of overwritten
void get_nums(int num, vector<int> &nums)
{
    // According to karaz conjecture: if it is even, then / 2
    if(num % 2 == 0)
    {
        num = num / 2;
    }
    // If it is an odd number, (3n+1)/2
    else
    {
        num = (3 * num + 1) / 2;
    }
    // Store the obtained number in nums
    nums.push_back(num);
    // End condition: num=1
    if(num == 1)
    {
        return ;
    }
    // If num is not equal to 1, the function is called recursively
    get_nums(num, nums);
}

// Gets the result array obtained after each circular deletion
void get_result(vector<int> nums, vector<int> &result_nums)
{
    // Traverse the covered number group to delete the corresponding number in the result array
    for(auto e : nums)
    {
        // Gets the length of the result array
        int len = result_nums.size();
        // If the number to be deleted is the last in the result array
        if(e == result_nums[len-1])
        {
            // Deletes the last number in the array
            result_nums.pop_back();
        }
        // If the number to be deleted is not the last of the result array
        else
        {
            // Find the corresponding value e in the result array
            vector<int>::iterator it = find(result_nums.begin(), result_nums.end(), e);
            // If the search is successful
            if(it != result_nums.end())
            {
                // Delete found number
                result_nums.erase(it);
            }
            // Or do nothing
        }
    }
}

Keywords: C++

Added by tsinka on Wed, 26 Jan 2022 14:22:57 +0200