poj 求二叉树的结点

#include <iostream>
#include <cmath>
using namespace std;

int find(int x)
{
   int i;
   for(i = 1; ;i++)
 {
     if(pow((double)2, i-1) <= x && x < pow((double)2, i))
        return i;
   }
}

int main()
{
 int n, high, m, low;
 int nCount, i;
 while (cin >> m >> n && m && n)
 {
  nCount = 0;
  low = find(m);
  high = find(n);

  if(low > high)
  {
        cout << 0 << endl;
        continue;
     }
     else if(high == low)
  {
        if(m <= n)
     cout << 1 << endl;
        else
      cout << 0 << endl;
        continue;
     }
     for(i = low; i < high; i++)
        nCount += (int)pow((double)2, i - low);
     int l = m, r = m;
  while(find(l)!= high)
       {
    l *= 2;
    r = r * 2 + 1;
      }
     if(n >= l)
  {
        if(n>r)
     nCount += r - l + 1;
        else
      nCount += n - l + 1;
     }
  cout << nCount << endl;
 }
 return 0;
}

posted @ 2012-04-18 19:47  逝者*恋世  阅读(127)  评论(0编辑  收藏  举报