problemscpp
A collection of my answers to algorithm problems in c++.
函数
pat::b::b1005 命名空间参考

1005 继续(3n+1)猜想 更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1005, case1)
 

详细描述

1005 继续(3n+1)猜想

函数说明

◆ main()

int pat::b::b1005::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp180 行定义.

180 {
181 int n;
182 cin >> n;
183 unordered_map<int, int> in(n);
184 for(int i = 0; i < n; i++) {
185 int num;
186 cin >> num;
187 in[num] = 0;
188 }
189 for(auto [num, deg]: in) {
190 int cpy = num;
191 if(deg == 0) {
192 while(cpy != 1) {
193 if(cpy % 2 == 0) {
194 cpy /= 2;
195 } else {
196 cpy = (cpy * 3 + 1) / 2;
197 }
198 if(in.contains(cpy)) {
199 in[cpy]++;
200 }
201 }
202 }
203 }
204 vector<int> ans;
205 for(auto [num, deg]: in) {
206 if(deg == 0) {
207 ans.push_back(num);
208 }
209 }
210 sort(ans.rbegin(), ans.rend());
211 for(int i = 0; i < ans.size(); i++) {
212 cout << ans[i];
213 if(i != ans.size() - 1) {
214 cout << ' ';
215 }
216 }
217 return 0;
218 }

被这些函数引用 TEST().

◆ TEST()

pat::b::b1005::TEST ( b1005  ,
case1   
)

在文件 pat_test.cpp74 行定义.

74 {
75 istringstream in("6\n"
76 "3 5 6 7 8 11");
77 auto out = ostringstream();
78 main(in, out);
79 const auto ans = out.str();
80 ASSERT_EQ("7 6", ans);
81 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().