// Problem 33
// 20 December 2002
//
// The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
//
// We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
//
// There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
//
// If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
#include <iostream>
#include <windows.h>
using namespace std;
// 获取一个2位数去掉aDigit后的另一个数字
// 如果范围不对或者这个数里没有aDigit,则返回-1
int GetAnotherDigit(int num, int aDigit)
{
if(num < 10 || num > 99) //范围不正确,返回-1
{
return -1;
}
if(num % 10 == aDigit) //如果个位相等,返回十位数
{
return num / 10;
}
if(num / 10 == aDigit) //如果十位相等,返回个位数
{
return num % 10;
}
return -1; //不存在,返回-1
}
// 判断double值是否相等
bool isEqual(double num1, double num2)
{
const static double range = 0.000001;
if(num1 - num2 < range && num1 - num2 > -range)
{
return true;
}
return false;
}
// 求2个数的最大公约数,用于分数化简
int GCD(int num1, int num2)
{
if(num1 <= 0 || num2 <= 0) //范围错误
{
return -1;
}
int remainder = 0; //余数
while(num2 != 0)
{
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
return num1;
}
void F1()
{
cout << "void F1()" << endl;
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
const int MIN_NUM = 10;
const int MAX_NUM = 99;
double value = 0.0; //分数的值
int simpleNumerator = 0; //化简后的分子
int simpleDenominator = 0; //化简后的分母
int productOfNumerator = 1; //分子的乘积
int productOfDenominator = 1; //分母的乘积
for(int numerator = MIN_NUM; numerator <= MAX_NUM; numerator++)
{
for(int denominator = numerator + 1; denominator <= MAX_NUM; denominator++) //分母比分子大
{
for(int commonDigit = 1; commonDigit <= 9; commonDigit++)
{
simpleNumerator = GetAnotherDigit(numerator, commonDigit); //分子去掉一个数
simpleDenominator = GetAnotherDigit(denominator, commonDigit); //分母去掉同一个数
if(simpleNumerator == -1 || simpleDenominator == -1) //如果无法去掉同一个数,则跳过
{
continue;
}
if(isEqual((double)numerator / denominator, (double)simpleNumerator / simpleDenominator)) //如果相等
{
cout << numerator << "/" << denominator << " = " << simpleNumerator << "/" << simpleDenominator << endl;
productOfNumerator *= numerator;
productOfDenominator *= denominator;
}
}
}
}
int commonFactor = GCD(productOfNumerator, productOfDenominator); //求最大公约数,用于化简分数
cout << "4个分数的乘积:" << productOfNumerator << "/" << productOfDenominator
<< " = " << productOfNumerator / commonFactor << "/" << productOfDenominator / commonFactor << endl;
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)((double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / (double)freq.QuadPart) << endl;
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
16/64 = 1/4
19/95 = 1/5
26/65 = 2/5
49/98 = 4/8
4个分数的乘积:387296/38729600 = 1/100
Total Milliseconds is 25.3444
By GodMoon
2011年11月4日15:16:52
*/
【ProjectEuler】ProjectEuler_033
最新推荐文章于 2024-08-04 14:55:04 发布