1005. Continuation (3n+1) conjecture
Karaz (Callatz) conjecture has been given a description in 1001. In this subject, the situation is slightly more complicated.
When we verify Karaz conjecture, in order to avoid repetition, we can record each number that is encountered in the recursive process. For example, when validating n=3 n=3, we need to calculate 3, 5, 8, 4, 2, 1, 3, 5, 8, 4, 2, 1, when we validate the n=5, 8, 4, 2 n=5, 8, 4, 2, we can directly determine the authenticity of the Karaz conjecture, and do not need to repeat the calculation, Because these 4 numbers have been encountered in the verification 3, we call 5, 8, 4, 2 5, 8, 4, 2 is the number of 3 3 "covered". We call a number n N in a sequence as "critical number" if n n cannot be overridden by other numbers in the sequence.
Now given a series of numbers to be validated, we just need to validate a few of the key numbers, so we don't have to re-validate the remaining numbers. Your task is to find these key numbers and output them in order from large to small.
input Format: each test input contains 1 test cases, the 1th line gives a positive integer k (<100) K (\lt 100), and line 2nd gives the value of k distinct positive integer n (1<n≤100) (1 \lt n \le 100). The numbers are separated by spaces.
output format: the output of each test case occupies one row, and the key numbers are output in order from large to small. The number is separated by 1 spaces, but there is no space after the last digit in a row.
Input Sample:
6
3 5 6 7 8 11
Sample output:
7 6 Ideas for solving problems
Notice the range of data in the topic, k<100,n<100 K, from the first question to speculate that the number of 1−100 between 1-100 is converted from this conjecture to 1 the most times estimate should be 97 this number (of course, can write a program to verify, I'm lazy. Not verified)
97 This conjecture takes approximately 75 conversions, remembering that the number of conversions is M M.
It can be said that if the complexity is O (100∗m) O (100*m), the problem is solvable.
then see how to Solve
Sample
6
3 5 6 7 8 All
We can save it in the form of an array of tokens
For example 7 this number, we let x[7]=1, not let x[i]=7, this can facilitate subsequent processing,
And because the data size is less than 100, the X array size is as large as 101. The
then scans the x array from the back forward from the large to the small, and if the x[i]=1 is encountered, the action on the topic of I is described.
and for all occurrences of the number a x[a] is 0,
to scan the x array from large to small, and the output of the remaining x[i in the X array as the value of 1 is the answer code
#include <stdio.h> #include <stdlib.h> #include <algorithm> using
namespace Std;
int x[200] = {0};
int half (int a) {int count = 0;
while (a > 1) {if (a% 2 = = 1) A = (A * 3 + 1)/2;
else a/= 2; if (a <) x[a] = 0;
Note that this place has an if statement to determine if a is outside the bounds of the array, otherwise the last two points on the Pat will report a segment error.
} return count;
} int main () {int n, in;
scanf ("%d", &n);
for (int i = 0; i < n; i++) {scanf ("%d", &in);
X[in] = 1;
} for (int i = 101; I >= 0; i--) {if (X[i]) half (i);
} int flag = 1; for (int i = 101; I >= 0; i--) {if (X[i]) {if (flag) {//This place is to match the output format of the topic, the title says that the last number is not empty after Grid printf ("%d", I);
Flag = 0;
} else {printf ("%d", I);
}}} return 0; }