首页 > 其他分享 >The 3n + 1 problem

The 3n + 1 problem

时间:2023-05-25 10:08:34浏览次数:34  
标签:integers max length numbers input problem cycle 3n


Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

 

Consider the following algorithm:


1. input n


2. print n

3. if n = 1 then STOP

4. if n is odd then 

5. else 

6. GOTO 2

 

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including iand j.

You can assume that no operation overflows a 32-bit integer.

Output

For each pair of input integers i and j you should output ij, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input


1 10 100 200 201 210 900 1000


Sample Output


1 10 20 100 200 125 201 210 89 900 1000 174


题意:
题的大意是输入两个数(注意,这里没有说第一个数一定要小与第二个数),然后对这两个数之间的所有整数包括端点的数,进行一种规律运算,并求出运算的次数,比较所有的数规律运算次数,输出最大的.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[1000010];
int main()
{
    int i, j, t, sum, num;
    while(scanf("%d%d", &i, &j) != EOF)
    {
        printf("%d %d ", i, j);
        if(i > j)
        {
            t = i;
            i = j;
            j = t;
        }
        memset(a,0,sizeof(a));
        int max = 0;
        for(i = i; i <= j; i++)
        {
            num = 0;
            sum = i;
            while(sum != 1)
            {
                if(sum % 2 == 0)
                {
                    sum = sum / 2;
                }
                else
                {
                    sum = 3 * sum + 1;
                }
                num++;
            }
            if(num > max)
            {
                max = num;
            }
            else
            {
                max = max;
            }
        }
        printf("%d\n", max + 1);
    }
    return 0;
}

 

标签:integers,max,length,numbers,input,problem,cycle,3n
From: https://blog.51cto.com/u_16127529/6345031

相关文章

  • 每日一题 力扣 1377 https://leetcode.cn/problems/frog-position-after-t-seconds/
    力扣1377https://leetcode.cn/problems/frog-position-after-t-seconds/这道题目用dp去做,构建邻接矩阵,做的时候需要注意题目条件,如果青蛙跳不动了,这个概率就保持不变了一般跳青蛙,很容易想到dp核心代码如下publicdoublefrogPosition(ipublicdoublefrogPosition(intn,......
  • AI的一致性问题(AI Alignment Problem)
    AI的一致性问题 (图片来源:维基百科,Kismetrobot。)人工智慧(AI)系统可以应用到很多方面,帮人类解决很多问题。但不论AI的原始目的是什么,万一AI发展出自己的功能或意识,做出预料之外的事,这可能会造成很多严重的后果,例如在很多电影里面变坏的机器人,试图控制人类的电脑等等。所以如何......
  • Codeforces Gym 103119B - Boring Problem(高斯消元)
    考虑建出AC自动机,朴素做法是高斯消元,\(f_i=\sum\limits_{j=0}^{k-1}f_{to_{i,j}}p_j+1\),复杂度\(O(n^3m^3)\),不能接受。考虑优化高斯消元的过程,我们定义以下节点为“关键点”:根节点对于一个trie树(也就是未经过AC自动机getfail操作得到的树)上有超过两个儿子的节点\(x......
  • June 2021-Continuous Transition: Improving Sample Efficiency for Continuous Cont
    摘要:尽管深度强化学习(RL)已成功应用于各种机器人控制任务,但由于样本效率较差,将其应用于现实世界任务仍然具有挑战性。为了克服这一缺点,一些工作侧重于在训练过程中重用收集的轨迹数据,将其分解为一组策略无关的离散变迁。然而,它们的改进有些边际,因为i)转换的数量通常很小,ii)值分......
  • 【git】报错解决方案-'This is probably not a problem with npm. There is likely ad
    git-commit报错: 原因:npm缓存造成的解决方案: 删除packpackage-lock.json,删除所有依赖,执行npmcacheclean--forcenpminstall......
  • 基于Maxwell建立的 8极12槽 110mm 外径 25mm 轴向长度 转速3000rpm 功率600W 转矩2.3N
    基于Maxwell建立的8极12槽110mm外径25mm轴向长度转速3000rpm功率600W转矩2.3Nm直流母线48V(直接连接在农村用的三轮车上面取电)永磁同步电机极其设计模型,转矩脉动小(PMSM和BLDC)。ID:7130623795626949......
  • AtCoder Beginner Contest 177 F I hate Shortest Path Problem
    洛谷传送门AtCoder传送门设\(f_{i,j}\)为从第\(1\)行到\((i+1,j)\)的最短路。因为我们并不关心最后到达的是哪一个格子,于是强制\(f_{i,j}\)为必须从\((i,j)\)往下走一格到\((i+1,j)\)的最短路。有转移:\[f_{i,r+1}\getsf_{i-1,j}+r+2-j,j\in[l......
  • 4.[1201D - Treasure Hunting](https://codeforces.com/problemset/problem/1201/D)
    4.1201D-TreasureHunting题目意思:在一个n*m的地图上面,左下角的坐标是(1,1),最开始你位于左下角,一秒钟你可以进行往左或者往右的操作,你只能在一些特殊的列上面进行往上移动的操作,你不可以往下移动。现在告诉你k个宝藏的坐标信息以及哪些列是允许往上的,问最后至少要几秒可以遍历k......
  • Problem E: 跳一跳
    ProblemDescription近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱。简化后的跳一跳规则如下:玩家每次从当前方块跳到下一个方块,如果没有跳到下一个方块上则游戏结束。如果跳到了方块上,但没有跳到方块的中心则获得1分;跳到方块中心时,若上一次的得分为1分或这是本局游戏的第一次......
  • 牛客 55994 2023牛客五一集训派对day3 D Points Construction Problem
    D-PointsConstructionProblem_2023牛客五一集训派对day3(nowcoder.com)将图上恰好\(n\)个点染成黑色,使得图上相邻的黑白点对数量恰好为\(m\)考虑\(n\)个黑点如果不相邻,则两个点的贡献互不影响考虑相邻的情况,我们把相邻的点连边,则贡献为每一个连通块的贡献的和,我们用......