挑戰編程 uva100 3n+1

挑戰編程 劉汝佳 的第一道習題  熱身題ios

熟悉下提交格式編程

題意ide

 

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 int n, m;
 7 
 8 
 9 
10 
11 
12 
13 int main()
14 {
15     while (cin >> n >> m) {
16         int start = min(n, m);
17         int end = max(n, m);
18         int maxcount = 0;
19         for (int i = start; i <= end; i++) {
20             long long tmp = i;
21             int count = 1;
22             while (tmp != 1) {
23                 if (tmp & 1) {
24                     //2
25                     tmp = tmp * 3 + 1;
26                 }
27                 else {
28                     //3
29 
30                     tmp = tmp >> 1;
31                 }
32                 count++;
33             }
34 
35             if (maxcount < count) maxcount = count;
36         }
37         cout << n << " " << m << " " << maxcount << endl;
38     }
39     
40 
41     return 0;
42 }
View Code
相關文章
相關標籤/搜索