1005 继续(3n+1)猜想 (25分)
标签:code 空格 没有 sort algorithm begin call -- 就是
卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。
当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。
例如对 n=3 进行验证的时候,我们需要计算 3、5、8、4、2、1,则当我们对 n=5、8、4、2 进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这 4 个数已经在验证3的时候遇到过了,我们称 5、8、4、2 是被 3“覆盖”的数。我们称一个数列中的某个数 n 为“关键数”,如果 n 不能被数列中的其他数字所覆盖。
现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。
每个测试输入包含 1 个测试用例,第 1 行给出一个正整数 K (<),第 2 行给出 K 个互不相同的待验证的正整数 n (1)的值,数字间用空格隔开。
每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用 1 个空格隔开,但一行中最后一个数字后没有空格。
6
3 5 6 7 8 11
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 }
标签:code 空格 没有 sort algorithm begin call -- 就是
原文地址:https://www.cnblogs.com/apprendre-10-28/p/12728434.html