poj-1207 The 3n+1 problem

 2019-11-30

思路很简单  注意那个找最大值,排序太费时间。学会了qsort😊

#include <stdio.h>
#include <stdlib.h>

#define N 1000005

int cycleLength[N];
int getCycleLength(int n)
{
    int counts=1;
    while(n>1)
    {
        if(n%2)
            n=3*n+1;
        else
            n/=2;
        counts++;
    }
    return counts;
}
int main()
{
    int i,j;
    for(i=0;i<=N;i++)
        cycleLength[i]=getCycleLength(i);
    while(scanf("%d%d",&i,&j)!=EOF && i!=0)
    {
        int maxCycleLength = 0;
        int k;
        for(k=i;k<=j;k++)
        {
            if(cycleLength[k]>maxCycleLength)
                maxCycleLength=cycleLength[k];
        }
        printf("%d %d %d\n",i,j,maxCycleLength);
    }
    return 0;
}

 

 

posted @ 2019-12-01 18:33  今天你AC了吗  阅读(118)  评论(0编辑  收藏  举报