[PAT B] Practise 1005 continues (3n + 1) guess

PAT (Basic Level) Practice (Chinese) 1005

1005 Continue (3n + 1) guess

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 = 3 for verification, we need to calculate 3,5,8,4,2,1, when we n when verifying = 5,8,4,2, 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 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, a first row is given a positive integer K (<100), the second line gives the K is a positive integer to be authenticated mutually different n- (1 < n- value ≦ 100) of the , 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

Ideas:

According to the topic can be analyzed:

Each test case for a number of calculations are performed Kharazi guess, if there is a number of other embodiments of the process used, put the number of labeled, finally embodiment that is not marked number 'key figures'.

Code:

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int n;
int tab[100][2] = { {0} };

void updata(int temp) {
    for (int i = 0; i < n; i++) 
        if (tab[i][0] == temp)
            tab[i][1] = 1;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;
        tab[i][0] = num;
    }
    for (int i = 0; i < n;i++) {
        int te = tab[i][0];
        while (te != 1) {
            if (te % 2 == 0) {
                updata(te / 2);
                te /= 2;
            }
            else {
                updata((3 * te + 1) / 2);
                te = (3 * te + 1) / 2;
            }
        }
    }
    int so[100] = { 0 };
    int t = 0;
    for (int i = 0; i < n; i++) {
        if (tab[i][1] != 1 && tab[i][0] != 0) {
            so[t] = tab[i][0];
            t++;
        }
    }
    sort(so, so+n);
    int k = 0;
    for (int i = n-1; i >=0 ; i--) {
        if (so[i] != 0 && i != 0) {
            if (k != 0)
                cout << ' ';
            cout << so[i];
            k++;
        }
    }
}

Guess you like

Origin www.cnblogs.com/eisuto/p/12422841.html