C ++ Continue (3n + 1) conjecture

1005 Continue (3n + 1) conjecture (25 points)
 

The Callatz conjecture has been described in 1001. In this subject, the situation is a bit more complicated.

When we verify the Karaz conjecture, in order to avoid repeated calculations, we can record every number encountered in the recursion process.

For example  , when verifying n = 3, we need to calculate 3, 5, 8, 4, 2, 1, then when we  verify n = 5, 8, 4, 2, we can directly determine the Karaz conjecture It is not necessary to repeat the calculation, because these 4 numbers have already been encountered when verifying 3. We call 5, 8, 4, 2 to be "covered" by 3. We call a certain number n in a sequence  as "key number", if  n cannot be covered by other numbers in the sequence.

Now given a series of numbers to be verified, we only need to verify a few of the key numbers, and we don't have to verify the remaining numbers again. Your task is to find these key figures and output them in order from largest to smallest.

Input format:

Each test input contains 1 test case, the first line gives a positive integer  K ( <), the second line gives  K different positive integers n ( 1) to be verified  , with spaces between the numbers Separate.

Output format:

The output of each test case occupies one line, and the key numbers are output in order from largest to smallest. The numbers are separated by a space, but there is no space after the last number in a row.

Sample input:

                                                                         6
                                                                         3 5 6 7 8 11
 

Sample output:

                                                                          7 6

 

 

 

 

 1 #include <iostream>
 2 
 3 #include <algorithm>
 4 #include <vector>
 5 #include <string>
 6 
 7 
 8 
 9 using namespace std;
10 
11 bool is_contain_this_num(vector<int> nums,int a)
12 {
13     for(int i = 0;i < nums.size();++i)
14     {
15         if(a == nums[i])
16         {
17             return true;
18         }
19     }
20     return false;
21 }
22 
23 
24 bool cmp(int s1,int s2)
25 {
26     return s1 > s2;
27 }
28 
29 int main()
30 {
31     int numcount ;
32     cin >> numcount;
33     vector<int> nums;
34     for(int i = 0;i < numcount;++i)
35     {
36         int num;
37         cin >> num;
38         nums.push_back(num);
39     }
40     
41     //存放经过检验的数
42     vector<int> check_vec;
43     for(int i = 0;i < nums.size();++i)
44     {
45         int n = nums[i];
46         if(!is_contain_this_num(check_vec,n))
47         {
48             while(n != 1){
49                 if(n%2 == 0)
50                 {
51                     n /= 2;
52                 }else
53                 {
54                     n = (3*n+1)/2;
55                 }
56                 if(!is_contain_this_num(check_vec,n))
57                 {
58                     check_vec.push_back(n);
59                 }
60             }
61         }
62     }
63 
64     vector<int> result;
65     for(int i = 0;i < nums.size();++i)
66     {
67         bool flag = false;
68         for(int j = 0;j < check_vec.size();++j)
69         {
70             if(nums[i] == check_vec[j])
71             {
72                 flag = true;
73             }
74         }
75         if(!flag)
76         {
77             result.push_back(nums[i]);
78         }
79     }
80     sort(result.begin(),result.end(),cmp);
81 
82     for(vector<int>::iterator it = result.begin();it != result.end();it++)
83     {
84         cout << *it;
85         if(it != result.end()-1)
86         {
87             cout << " ";
88         }
89     }
90     return 0;
91 }

 

Guess you like

Origin www.cnblogs.com/apprendre-10-28/p/12728434.html