// 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
*/
【ProjectEuler】ProjectEuler_040
最新推荐文章于 2024-04-15 10:46:09 发布