【ProjectEuler】ProjectEuler_041

// Problem 41
// 	11 April 2003
//
// 	We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
//
// 	What is the largest n-digit pandigital prime that exists?

#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;


//************************************
// Method:    IsPandigital
// FullName:  IsPandigital
// Describe:  检查num是否为Pandigital
// Access:    public
// Returns:   bool
// Qualifier:
// Parameter: int num
//************************************
bool IsPandigital(int num, int n)
{
    bool exsitDigit[9] = {false};		//位bit,用于记录存在的位
    int currentDigit = 0;

    while(num != 0)
    {
        currentDigit = num % 10;

        if(currentDigit == 0)			//含0,不符合要求
        {
            return false;
        }

        if(currentDigit > n)			//数字大于当前位数,不符合要求
        {
            return false;
        }

        if(exsitDigit[currentDigit - 1])	//已存在这个数字
        {
            return false;
        }

        exsitDigit[currentDigit - 1] = true;	//记录
        num /= 10;
    }

    return true;
}

//************************************
// Method:    IsPrimeNum
// FullName:  IsPrimeNum
// Describe:  判断某数是否为素数
// Access:    public
// Returns:   bool
// Qualifier:
// Parameter: int num
//************************************
bool IsPrimeNum(int num)
{
    if((num % 2 == 0 && num > 2) || num <= 1)
    {
        return false;
    }

    int sqrtNum = (int)sqrt((double)num);

    for(int i = 3; i <= sqrtNum; i += 2)
    {
        if(num % i == 0)
        {
            return false;
        }
    }

    return true;
}

//************************************
// Method:    F1
// FullName:  F1
// Describe:  这个算法比较糟糕,是最愚笨的遍历,十分耗时
// Access:    public
// Returns:   void
// Qualifier:
//************************************
void F1()
{
    cout << "void F1()" << endl;

    LARGE_INTEGER timeStart, timeEnd, freq;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&timeStart);

    const int MAX_NUM = 987654321;		//最大的Pandigital

    int numCount = 9;					//当前数字的位数
    int nextDecrease = 100000000;		//下个位数改变点

    int result = 0;						//找到的结果

    //从大到小遍历所有数
    for(int i = MAX_NUM; i > 0; i -= 2)
    {
        //找到需要的数字
        if(IsPandigital(i, numCount) && IsPrimeNum(i))
        {
            result = i;
            break;
        }

        //位数改变
        if(i < nextDecrease)
        {
            nextDecrease /= 10;
            --numCount;
        }
    }

    cout << "结果为:" << result << endl;

    QueryPerformanceCounter(&timeEnd);
    cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;

    time_t currentTime = time(NULL);
    char timeStr[30];
    ctime_s(timeStr, 30, ¤tTime);
    cout << endl << "By GodMoon" << endl << timeStr;
}

//主函数
int main()
{
    F1();
    return 0;
}

/*
void F1()
结果为:7652413
Total Milliseconds is 148553

By GodMoon
Wed Apr 11 12:58:45 2012
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值