Project Euler——Problem 33 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 32
Project Euler——Problem 34

Project Euler——Problem 33

daybreakcx posted @ 2009年8月16日 23:04 in Prject Euler , 1144 阅读

原题与答案:

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.

Answer:100

 

分析与解答:

这个题目是说形如\dfrac{ab}{bc}=\dfrac{a}{c}的小于1的分数一共有四个,其中ab和bc均为两位数,问这四个分数的乘积的最简分式的分母是什么。这个题目很明显是一个枚举题,这里要用到的就是一个最大公约数的过程,求最大公约数我们可以利用辗转相除法来实现,然后分别依次枚举b,a和c,要注意的是三个数都必须非0,而且c>a,实际上这题的实现很简单,具体的程序源代码如下所示:

  1. #include<stdio.h>
  2. int i,j,k,s,t,num,den;
  3. int gcd(int x,int y)
  4. {
  5.         int t;
  6.         while (t=x%y)
  7.         {
  8.                 x=y;
  9.                 y=t;
  10.         }
  11.         return y;
  12. }
  13. int main()
  14. {
  15.         num=den=1;
  16.         for (i=1;i<10;i++)
  17.                 for (j=1;j<10;j++)
  18.                         for (k=j+1;k<10;k++)
  19.                         {
  20.                                 s=gcd(j*10+i,i*10+k);
  21.                                 t=gcd(j,k);
  22.                                 if ((j*10+i)/s==j/t&&(i*10+k)/s==k/t)
  23.                                 {
  24.                                         num*=j;
  25.                                         den*=k;
  26.                                 }
  27.                         }
  28.         printf("%d\n",den/gcd(num,den));
  29.         return 0;
  30. }

 

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter