UVa100 The 3n + 1 problem

来源:互联网 发布:现货分时指标公式源码 编辑:程序博客网 时间:2024/06/10 17:56
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36
注意 i j的大小不定,得调整。国外的都这么坑吗。

#include<iostream>
#define ll long long
using namespace std;
int s(ll n)
{
int c;
for(c=1;n!=1;c++)
{
if(n&1)n=n*3+1;
else n/=2;
}
return c;
}

int main()
{
int a,b,t;
while(cin>>a>>b)
{
cout<<a<<" "<<b<<" ";
if(a>b){t=a;a=b;b=t;}
int Max=-1;
for(int i=a;i<=b;i++)
{
int k=s(i);
if(k>Max) Max=k;
}
cout<<Max<<endl;
}
}


0 0
原创粉丝点击