Friday, May 8, 2009

[PC]3n+1

#include 
#include 
#include 

using namespace std;

typedef unsigned int uint32;

class MaxCycle
{
private:
 int  max_;
 int  count_;
public:
 MaxCycle() : max_(0), count_(0)
 {
 }

 void operator()(uint32 ele)
 {
  count_ = 0;
  while(1 < ele)
  {
   ele = ele % 2 ? 3*ele+1 : ele / 2;
   ++count_;
  }

  if(max_ < count_+1)
   max_ = count_+1;
 }

 int max()
 {
  return max_;
 }
};

int main()
{
 int a, b;
 int s, len;

 while(cin >> a >> b)
 {
  if(a <= b)
  {
   s = a;
   len = b-a+1;
  }
  else
  {
   s = b;
   len = a-b+1;
  }

  vector coll;

  for(int i=s; i < s+len; ++i)
  {
   coll.push_back(i);
  }

  MaxCycle mc = for_each(coll.begin(), coll.end(), MaxCycle());

  cout << a << " " << b << " " << mc.max() << endl;
 }
 return 0;
}

No comments:

Post a Comment