【ProjectEuler】ProjectEuler_040

// Problem 40
// 	28 March 2003
//
// 	An irrational decimal fraction is created by concatenating the positive integers:
//
// 0.123456789101112131415161718192021...
//
// 	It can be seen that the 12th digit of the fractional part is 1.
//
// 	If dn represents the nth digit of the fractional part, find the value of the following expression.
//
// 	d1  d10  d100  d1000  d10000  d100000  d1000000

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

//************************************
// Method:    GetNumPosition
// FullName:  GetNumPosition
// Describe:  获取num的从低位往高位数,第position位置的数字,position从0开始
// Access:    public
// Returns:   int
// Qualifier:
// Parameter: int num
// Parameter: int position
//************************************
int GetNumPosition(int num, int position)
{
    for(int i = 0; i < position; ++i)
    {
        num /= 10;
    }

    return num % 10;
}

void F1()
{
    cout << "void F1()" << endl;

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

    int position = 1;						//要寻找的位置,每次自乘10

    const int MAX_POSITION = 1000000;		//最大的位置

    int currentNum = 1;						//当前的数字
    int currentNumLength = 1;				//当前的数字的位数
    int nextIncrese = 10;						//下次进位的值
    int currentPosition = 1;				//当前的位置

    int result = 1;							//乘积结果

    while(position <= MAX_POSITION)
    {
        //如果达到了要寻找的位置
        if(currentPosition >= position)
        {
            result *= GetNumPosition(currentNum, currentPosition - position);
            position *= 10;
        }

        //当前计数+1
        ++currentNum;

        //如果位数变多了,则长度+1
        if(currentNum == nextIncrese)
        {
            ++currentNumLength;
            nextIncrese *= 10;
        }

        //当前位置增加
        currentPosition += currentNumLength;
    }

    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()
结果为210
Total Milliseconds is 2.29293

By GodMoon
Wed Apr 11 12:02:55 2012
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值