首页 > 其他 > 详细

POJ 1207 The 3n + 1 problem

时间:2016-01-22 17:28:52      阅读:97      评论:0      收藏:0      [点我收藏+]

C++11 feature version

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const unsigned maximum = 10000;

unsigned loop(unsigned n)
{
    unsigned i = 1;

    while (n != 1)
    {
        i++;
        if (n % 2)
            n = n * 3 + 1;
        else
            n = n / 2;
    }

    return i;
}

int main()
{
    vector<unsigned> v(maximum);

    for (unsigned i = 0; i < maximum; i++)
        v[i] = loop(i + 1);

    int i, j;

    while (cin >> i >> j)
    {
        vector<unsigned>::const_iterator runner = v.cbegin() + (i > j ? i : j);
        vector<unsigned>::const_iterator chaser = v.cbegin() + (i > j ? j : i);

        unsigned maxElem = 0;

        for_each(chaser, runner, [&maxElem](const unsigned e)
        {
            if (e > maxElem)
                maxElem = e;
        });

        cout << maxElem << endl;
    }

    return 0;
}

 

POJ 1207 The 3n + 1 problem

原文:http://www.cnblogs.com/fengyubo/p/5151402.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!