码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1207 The 3n + 1 problem

时间:2016-01-22 17:29:43      阅读:125      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!