1207:The 3n + 1 problem

来源:互联网 发布:java中网络编程详解 编辑:程序博客网 时间:2024/06/11 06:27

题目链接:http://poj.org/problem?id=1207

方法:简单模拟

思路:好吧,我承认是因为今天HDUoj系统关闭了,只好试试poj了,这个题不难,水题,折腾那么长时间我也是醉了,按照题意简单模拟就可以了,但是要注意一点,给出数据的大小关系不定,是需要自己判定的,不知道题目哪里说明的,反正我是没看见,眼瞎。

难点:审题

#include <iostream>#include<climits>using namespace std;int findlen(int x){    int num = 0;    while(x != 1)    {        if(x%2 == 0)            x /= 2;        else            x = 3*x+1;        num++;    }    return num+1;}int main(){    int a,b;    while(cin>>a>>b)    {        cout<<a<<" "<<b<<" ";        if(a>b)        {            int t;            t = a;            a = b;            b = t;        }        int maxmum =INT_MIN ;        for(int i = a;i <= b;i++)        {            if(findlen(i) > maxmum)                maxmum = findlen(i);        }        cout<<maxmum<<endl;    }}


0 0
原创粉丝点击