Beginners pat basic exercises five to continue (3n + 1) guess

topic:

Kharazi (Callatz) conjecture description has been given in 1001. In this topic, the situation is somewhat more complicated.

When we verify Kharazi guess when, in order to avoid double counting, the number can be recorded at each recursive encountered. For example, when n = 3 for verification, we need to calculate 3,5,8,4,2,1, then when we verify the n = 5,8,4,2, it can be determined directly Kharazi guess authenticity, without double counting, because it has the number 4 3 when encountered in the validation, we are called 5,8,4,2 3 "coverage" of the number. We call a number in a column n is the number of "key number", if n can not be covered by other figures in the series.

Now given to a series of numbers to be verified, we only need to verify a few key number of them, you do not have to be repeated to verify the remaining numbers. Your task is to find these key figures, according to output them in descending order.

Input formats:

Positive integer value of n (1 <n≤100) Each test comprises a test input, a first row is given a positive integer K (<100), the second line gives the K different from each other to be verified , separated by spaces between numbers.

Output formats:

Each test case output per line, in descending order of output key figures. Between numbers separated by a space, but the row is not the last digit space

Sample input:

6
3 5 6 7 8 11

 

Sample output:

7 6

Ideas:

The core idea is to select the array number does not meet the conditions, then deleted, where the number of ineligible when the array is doing Kharazi guess there have been few, so use this as a judgment condition, and needs to be removed, so need to dynamically adjust the array to die, so use the vector container

Code:

#include<iostream>
#include<vector>
#include <algorithm>//包含sort函数,find函数
using namespace std;
int main()
{
    int num;
    vector <int> vec;
    cin>>num;
    while(num--)
    {
        int temp;
        cin>>temp;
        vec.push_back(temp);
    }
    vector<int> vec_temp(vec);//声明一个复制的vector,为了删除元素
    for(vector<int>::iterator it_temp,it=vec_temp.begin();it!=vec_temp.end();it++)
    {
        while(*it!=1)
        {
            if(*it%2==0)
            {
                *it/=2;
            }
            else
            {
                *it=(3*(*it)+1)/2;
            }
            it_temp=find(vec.begin(),vec.end(),*it);
            if(it_temp!=vec.end())
                vec.erase(it_temp);
        }
    }
    sort(vec.begin(),vec.end(),greater<int>());
    for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
    {
        cout << *it;
        if(it!=vec.end()-1)
            cout<<" ";
    }
    return 0;
}

Experience:

 For the vector, it is a dynamic array, instead of directly inserting or deleting an element while traversing it, otherwise it would iterator disorder, but should be traversing a copy, and then delete an element in the original container

vector of the initial value is completed at the time of the declaration, not just the first statement in the following initial value (of course I did not push_back as initial value)

Vector () is passed a pointer when the initial value, and the scope of the left and right to open and close (not including the right pointer)

find () function returns a pointer to the iterator

vector.erase () is passed an iterator

sort, find the algorithm header file

sort () sorting third parameter, greater <int> () is descending, less <int> () is ascending

Published 16 original articles · won praise 3 · Views 686

Guess you like

Origin blog.csdn.net/qq_40930559/article/details/104071465