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

1005 Continue (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

analysis:

创建一个足够大的数组 recover_arr[MAXSIZE] (初始化为0),对每个输入的数字进行验证(3n+1猜想的步骤),验证过的数字i令 recover_arr[i] = 1 (标记为1),最后对这些输入的数字从大到小排序,输出 recover_arr[arr[i]] == 0; 的数字就可以了

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #define MAXSIZE 10000
 5 using namespace std;
 6 
 7 
 8 
 9 int main()
10 {
11     int n;
12     vector<int> arr;
13     int recover_arr[MAXSIZE] = { 0 };
14     cin >> n;
15     int temp_input = 0;
16     for (int i = 0; i < n; ++i)
17     {
18         cin >> temp_input;
19         arr.push_back(temp_input);
20         int temp = arr[i];
21         while (temp != 1)
22         {
23             if (temp % 2 == 0)
24             {
25                 temp /= 2;
26             }
27             else
28             {
29                 temp = (3TEMP + * . 1 ) / 2 ;
 30              }
 31 is              recover_arr [TEMP] = . 1 ;
 32          }
 33 is      }
 34 is      Sort (arr.begin (), arr.end ());         // in ascending order 
35      Reverse (arr.begin (), arr.end ());     // array reverse, descending order 
36      int In Flag = 0 ;
 37 [      for ( int I = 0 ; I <n-; ++ I)
 38 is      {
 39          IF (recover_arr [ ARR [I]]! = . 1 )
40         {
41             if (flag == 1)
42             {
43                 cout << " ";
44             }
45             cout << arr[i];
46             flag = 1;
47         }
48     }
49     return 0;
50 }

 

 

Guess you like

Origin www.cnblogs.com/47Pineapple/p/11374812.html