PAT Basic 1005 continues (3n + 1) guess (25 points)

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  n- = time to verify 3, we need to calculate 3,5,8,4,2,1, when we  n- = time for verification 5,8,4,2, can be directly determined 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 the column of a number  n is "critical 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:

Each test comprises a test input, the first row is given a positive integer  K ( <), the second row is given  a positive integer number K to be authenticated mutually different  n ( from 1) value, a space number separated.

Output formats:

Each test case output per line, in descending order of output key figures. Separated by a space between the numbers, but after a row last number with no spaces.

Sample input:

6
3 5 6 7 8 11

Sample output:

7 6



#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void validate(int a,vector<int>& vec){
    while(a!=1){
        int pp=-1;
        if(a%2==1) a=a*3+1;
        a/=2;
        for(int i=0;i<vec.size();i++){
            if(vec[i]==a) pp=i;
        }
        if(pp!=-1) vec.erase(vec.begin()+pp);
    }
}
int main(){
    int T,tmp;
    vector<int> vec;
    cin>>T;
    while(T--){
        cin>>tmp;
        vec.push_back(tmp);
    }
    vector<int> vec2=vec;
    for(int i=0;i<vec.size();i++){
        validate(vec[i],vec2);
    }
    sort(vec2.begin(),vec2.end(),greater<int>());
    for(int i=0;i<vec2.size();i++){
        cout<<vec2[i];
        if(i!=vec2.size()-1) cout<<" ";
    }
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11355678.html