博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Project Euler] Problem 60 Deep First, Milestone, New Start
阅读量:5167 次
发布时间:2019-06-13

本文共 3308 字,大约阅读时间需要 11 分钟。

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
*> g_map;vector
* g_pVec;void Problem_60(){ InitPrimes(); for(int i=0; i
; } g_pVec->push_back(g_primes[j]); } } if(g_pVec) { g_map.insert(pair
*>(g_primes[i], g_pVec)); } } struct Step { //map
*>::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
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
* pVec = g_map[key]; int nextKey = (*pVec)[index]; bool isNextKeyOK = true; list
::iterator iterStep = vecStep.begin(); list
::iterator iterLastStep = vecStep.end(); iterLastStep--; while(iterStep != iterLastStep) { vector
* pCur = g_map[iterStep->Key]; vector
::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
*>::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
::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

你可能感兴趣的文章
[置顶] ListBox控件的数据绑定
查看>>
链表插入排序
查看>>
http://blog.csdn.net/yunye114105/article/details/7997041
查看>>
设计模式这个东西 刚刚发现几种模式好像大同小异啊
查看>>
关于 主键和外键
查看>>
python集合的交,差,并,补集合运算汇总
查看>>
校园分期支付的机遇和风险
查看>>
怕忘记-windows 2003服务器安装Node.js NPM
查看>>
一鍵分享(優化后)
查看>>
dcm4che 的依赖无法下载
查看>>
cygwin主要命令
查看>>
多线程存在哪些风险
查看>>
洛谷P2692 覆盖 题解
查看>>
Linux下清理内存和Cache方法见下文:
查看>>
【AngularJs-模块篇-Form篇】
查看>>
支持向量基
查看>>
单链表 类
查看>>
类的组合 构造函数的用法
查看>>
ORACLE SQL:经典查询练手第三篇
查看>>
ubuntu 包管理
查看>>