UVA 694 The Collatz Sequence

来源:互联网 发布:电商公司数据分析 编辑:程序博客网 时间:2024/05/18 19:36

UVA 694 The Collatz Sequence

题意:输入一个数A(A<2147483647),和一个上限limit,进行以下步骤:
Step1:
任选一个正整数A作为这个数列的第一项。
**Step2:
如果A=1则停止。**
Step3:
如果A为偶数,则A=A/2然后重新回到Step2。
Step4:
如果A为奇数,则A=3*A+1然后重新回到Step2。

Sample Input:
<3 100
34 100
75 250
27 2147483647
101 304
101 303
-1 -1
Sample Output:

//UVA 694//The Collatz Sequence//by Yanx#include<cstdio>#include<iostream>using namespace std;int main(){    long long n,limit,count=1;    while(~scanf("%lld%lld",&n,&limit)&&n!=-1&&limit!=-1)    {        long long  a=n;        long long  step=0;        while(a!=1&&a<=limit)        {            if(a%2==0) {a/=2;step++;}            else if(a%2==1){a=3*a+1;    step++;}            if(a==1)step++;          }        printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",count++,n,limit,step);    }}
0 0
原创粉丝点击