UVA-694-限制性的3n+1问题

本文介绍了一个类似于3n+1问题的算法实现,该算法在达到特定限制时停止递归过程。通过使用C++编程语言,文章详细展示了如何编写用于计算序列长度的函数,并提供了一个主函数来读取输入并打印结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//这个题目的大意是类似3n+1问题的,不过就是对于3n+1中.如果3n+1>给出的限制.就直接break了..

#include<stdio.h>

#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<vector>
const int inf = 0x3f3f3f;
using namespace std;
long long int compute(long long int x, long long int y)
{
    long long int cnt = 1;
    long long int mm;
    while(x-1)
    {
        if( x % 2 )
        {
            mm = x*3+1;
            if(mm <= y)
              x = x*3+1;
            else
             {
                 x = 1;cnt--;
             }
        }
        else
        {
            x = x/2;
        }
        cnt++;
    }
        return cnt;
}
int main()
{
    long long int a,l,t = 1;
    while(scanf("%lld%lld",&a,&l) && a + l != -2)
    {
        int m = compute(a,l);
        printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",t,a,l,m);
        t++;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值