close

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

#include <iostream>

using namespace std;

int condition(int n, int length)
{
    if(n==1)
    {
        return length;
    }
    else if(n%2==1)
    {
        n=(n*3)+1;
        condition(n, ++length);
    }
    else if(n%2==0)
    {
        n/=2;
        condition(n, ++length);
    }
}
int main()
{
    int i,j;
    while(cin>>i>>j)
    {
        cout<<i<<" "<<j<<" ";
        int max=0;
        if(i>j)
        {
            int temp=i;
            i=j;
            j=temp;
        }
        for(i;i<=j;i++)
        {
            int length=1;
            length=condition(i,length);
            if(max<length)
            {
                max=length;
            }
        }
        cout<<max<<endl;
    }
}

 

 

※查詢所有題目及解答如下※

https://jennaweng0621.pixnet.net/blog/post/403629785-CPE%E9%A1%8C%E7%9B%AE%E7%9B%AE%E9%8C%84%E5%8F%8A%E8%A7%A3%E7%AD%94

※查詢其他CPE題目如下※

http://jennaweng0621.pixnet.net/blog/category/6372301

arrow
arrow
    文章標籤
    UVA100 The 3n+1 problem
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()