4. PAT 1005 continues (3 n + 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  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

Ideas:
First, understand Kharazi guess, that if the current number is even, then cut off half of its re-assigned to it, if the current number is odd, then cut down by half (3n + 1) re-assigned to it, the final number It becomes 1.
Each number in the process is changed to 1, will produce some numbers, these numbers we need to record table. The next time computing another number, this lookup table from the current figures whether there have been, if there have been, it can directly determine conjecture.
For example, when n = 3, n vary from 3 to 1 in the process, resulting in 5, 8, 4, 2, 1. It should be noted that the current initial number, and can not be recorded in the table.
With the number of digits to record the variable K to be treated,
with the array of recording digital input to be processed (using dynamic allocation, delete the last free space required),
with the result type bool array to record each number appears in the middle of the process ,
followed by processing each number will result into the result array.
Title descending output Finally, the sort function can be used, and customize a descending compare function, as shown in the following code. It can also be used to set the collection process, the set int, greater <int >> can be realized <.
CMP BOOL (A int, int B)
{
  return A> B;
}
Sort (A, + n-A, CMP);
by these lines, you can descending order.

Code:
#include <iostream>
#include <set>
using namespace std;

int main() {
cout << "Enter K : ";
int k = 0;
int *input = new int[k];
cin >> k;

for (int i = 0; i < k; ++i) {
cout << "Enter : ";
cin >> input[i];
}

bool result[1000] = {false};

for (int j = 0; j < k; ++j) {
int current_num = input[j];
while (current_num != 1) {
if (current_num % 2 == 0) {
current_num /= 2;
result[current_num] = true;
} else {
current_num = (3 * current_num + 1) / 2;
result[current_num] = true;
}
}

}

set<int, greater<int>> key_num;
for (int i = 0; i < k; ++i) {
if (!result[input[i]]) {
key_num.insert(input[i]);
}
}

for (auto x : key_num) {
cout << x << " ";
}

delete [] input;

return 0;
}


Guess you like

Origin www.cnblogs.com/Hello-Nolan/p/12099962.html