[B] continue PAT (3n + 1) guess

Subject description:

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. 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

Problem-solving ideas:

This question is similar to the first question

Code:

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    int a[101]={0},h[10000]={0};
    int n,x,num=0;
    cin>>n;
    for(int i=0;i<n;i++) {
        cin>>a[i];
        x=a[i];
        if(h[x]==1) {
            continue;
        }
        while(x>1) {
            if(x%2==0) {
                x=x/2;
            } else {
                x=(3*x+1)/2;
            }
            h[x]=1;
        }
    }
    sort(a,a+n);
    for(int i=n-1;i>=0;i--) {
        if(h[a[i]]==0) {
            num++;
            if(num>1)
                cout<<" ";
            cout<<a[i];
        }
    }
    return 0;
}
Published 55 original articles · won praise 30 · views 9809

Guess you like

Origin blog.csdn.net/chaifang0620/article/details/104913674