int 中连续 1 的个数,并且求左右边界。
1 #include <iostream>
2 using namespace std;
3
4 int foo(int n, int& l, int& r)
5 {
6 int ret = 0, now = 0;
7 int l2, r2;
8 r2 = 0;
9 l2 = -1;
10 l = r = l2;
11 int idx = 0;
12 while (n != 0)
13 {
14 if (n % 2 != 0)
15 {
16 ++now;
17 l2 = idx;
18 if (now == 1)
19 {
20 r2 = idx;
21 }
22 }
23 else
24 {
25 now = 0;
26 }
27 if (now > ret)
28 {
29 ret = now;
30 l = l2;
31 r = r2;
32 }
33 ++idx;
34 n /= 2;
35 }
36 return ret;
37 }
38
39 int main()
40 {
41 int n;
42 while (cin >> n)
43 {
44 int ret, l, r;
45 ret = foo(n, l, r);
46 cout << ret << ' ' << r << ' ' << l << endl;
47 }
48 return 0;
49 }
posted on 2011-05-21 11:30
unixfy 阅读(137)
评论(0) 编辑 收藏 引用