1001. 害死人不偿命的(3n+1)猜想 (15)

来源:互联网 发布:贷款那个软件好 编辑:程序博客网 时间:2024/05/16 09:13
public class TestFn {
    public static void main(String[] args) {//测试窗口
        TestFn fn = new TestFn();
        for (int i = 2; i <100; i++) {
            int[] count = fn.count(i);
            System.out.println(i+"--[步数,经过的最大值]--"+Arrays.toString(count));
        }
    }
    int[] count(int num){//给一个数,返回所需要的走的步骤放置在索引0即x[0];所覆盖是最大值索引1即x[1]
        int[] x={0,num,num};
        while(x[1]!=1){
            countfn(x);
        }
        return new int[]{x[0],x[2]};
    }
    int[] countfn(int[] x){//索引0计数,索引1值,索引2为覆盖最大值-->执行下一步的变化
        if(x[1]%2==0){
            x[1]=x[1]/2;
            x[0]++;
        }else{
            int i=x[1]*3+1;
            x[2]=x[2]>i?x[2]:i;
            x[1]=i/2;
            x[0]++;
        }
        return x;
    }

}

运行结果:



0 0
原创粉丝点击