首页 > 代码库 > UVA100 The 3n + 1 problem

UVA100 The 3n + 1 problem

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

简单模拟题。

#include<stdio.h>
#include<iostream>
using namespace std;
int len[1000001];
int main()
{
    int i,a,b,a1,b1;
    long long j;
      for (i=1;i<=1000000;i++)
        {
           j=i;
           int k=1;
           while (j!=1)
            {
                if (!(j&1)) j=j>>1;
                else j=j*3+1;
                k++;
            }
           len[i]=k;
        }
    while (scanf("%d%d",&a,&b)!=EOF)
    {
    int temp=0;
    a1=a;b1=b;
     if (a>b)
     {
         temp=a;
         a=b;
         b=temp;
     }
     int maxx=len[a];
     for (i=a+1;i<=b;i++)
       if (maxx<len[i]) maxx=len[i];
          printf("%d %d %d\n",a1,b1,maxx);
    }
    return 0;
}


UVA100 The 3n + 1 problem