[Project Euler] Problem 60 Deep First, Milestone, New Start

Problem Description

The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

C++

This problem is interesting! I use Deep First Tradegy to resolve this problem.

const int MAX_NUM = 10000;
int* g_primes = NULL;
int g_length = 0;

void InitPrimes()
{
    g_length = MAX_NUM / 5;
    g_primes = new int[g_length];
    MakePrimes(g_primes, g_length, MAX_NUM);
}

struct NumberWithWeight
{
    int Number;
    int Weight;
    NumberWithWeight(int num, int weight)
        : Number(num), Weight(weight)
    {

    }
    NumberWithWeight()
        : Number(0), Weight(1)
    {

    }
};

NumberWithWeight* g_result = new NumberWithWeight[5];

NumberWithWeight GetNumberWeight(int num)
{
    int weight = 10;
    while(num / weight != 0)
    {
        weight *= 10;
    }
    return NumberWithWeight(num, weight);
}


bool CheckIsPrime(const NumberWithWeight& num1, const NumberWithWeight& num2)
{
    int temp = num1.Number * num2.Weight +  num2.Number;
    if(!IsPrime(temp))
    {
        return false;
    }
    temp = num2.Number * num1.Weight + num1.Number;
    return IsPrime(temp);
}

map<int, vector<int>*> g_map;
vector<int>* g_pVec;

void Problem_60()
{
    InitPrimes();

    for(int i=0; i<g_length - 1; i++)
    {
        g_pVec = NULL;
        for(int j=i +1; j <g_length; j++)
        {
            NumberWithWeight nw1 = GetNumberWeight(g_primes[i]);
            NumberWithWeight nw2 = GetNumberWeight(g_primes[j]);

            if(CheckIsPrime(nw1, nw2))
            {
                if(!g_pVec)
                {
                    g_pVec = new vector<int>;
                }
                g_pVec->push_back(g_primes[j]);
            }
        }
        if(g_pVec)
        {
            g_map.insert(pair<int, vector<int>*>(g_primes[i], g_pVec));
        }
    }

    struct Step
    {
        //map<int, vector<int>*>::iterator Key;
        int Key;
        int Index;

        Step(int key, int index)
            : Key(key), Index(index)
        {
        }

        bool Equals(const Step& other)
        {
            return ((Key == other.Key) && (Index == other.Index));
        }

        bool operator== (const Step& other)
        {
            return Equals(other);
        }

        bool operator != (const Step& other)
        {
            return !Equals(other);
        }
    };

    list<Step> vecStep;
    vecStep.push_back(Step(g_map.begin()->first, 0));
    while(vecStep.size() <5)
    {
        assert(!vecStep.empty());
        Step lastStep = vecStep.back();

        int index = lastStep.Index;
        int key = lastStep.Key;
        vector<int>* pVec = g_map[key];

        int nextKey = (*pVec)[index];

        bool isNextKeyOK = true;

        list<Step>::iterator iterStep = vecStep.begin();
        list<Step>::iterator iterLastStep = vecStep.end();
        iterLastStep--;
        while(iterStep != iterLastStep)
        {
            vector<int>* pCur = g_map[iterStep->Key];
            vector<int>::iterator retIter = find(pCur->begin(), pCur->end(), nextKey);
            if(retIter == pCur->end())
            {
                isNextKeyOK = false;
                break;
            }
            iterStep++;
        }
        isNextKeyOK = isNextKeyOK & (bool)g_map.count(nextKey);

        if(isNextKeyOK)
        {
            vecStep.push_back(Step(nextKey, 0));
            continue;
        }
        else
        {
            index++;

            while(index >= pVec->size())
            {

                vecStep.pop_back();
                if(vecStep.empty())
                {
                    map<int, vector<int>*>::iterator last = g_map.find(lastStep.Key);
                    last++;
                    if(last != g_map.end())
                    {
                        vecStep.push_back(Step(last->first, 0));
                    }
                }
                lastStep = vecStep.back();
                index = lastStep.Index;
                key = lastStep.Key;
                pVec = g_map[key];

                index++;
            }
            vecStep.back().Index = index;
            continue;

        }


    }

    int result = 0;

    list<Step>::iterator iterStep = vecStep.begin();
    while(iterStep != vecStep.end())
    {
        result += iterStep->Key;
        iterStep++;
    }

    getchar();
}

I‘m going to put Project Euler away for a while, cause more meanning work is waiting for me.
原文链接: https://www.cnblogs.com/quark/archive/2012/08/16/2642981.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/59663

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午9:11
下一篇 2023年2月9日 上午9:12

相关推荐