您的位置:首页 > 产品设计 > UI/UE

694 - The Collatz Sequence

2013-07-29 17:29 246 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=635

#include <iostream>
#include <cstring>
using namespace std;
int nums[1024];
int main()
{
memset(nums,0,sizeof(nums));
long c=0,a,l,term,n; //要使用long 虽然提示输入的a不会 大于 int的最大值 但在运算3*a+1时会超出
while(cin >> a >> l && a!=-1 && l!=-1)
{
c++;
n=a;
term = 0;
while(a!=1)
{
nums[term]=a;
term++;
if(a%2==0)
{
a /= 2;
}
else
{
a = 3*a+1;
}
if(a > l)
{
term--;
break;
}
}
if(n!=1)
term++;
cout << "Case "<< c << ": A = "<< n << ", limit = "<< l<< ", number of terms = " << term << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 算法 UVa