[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值