zoukankan      html  css  js  c++  java
  • Project Euler:Problem 33 Digit cancelling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/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>
    using namespace std;
    
    int gcd(int a, int b)
    {
    	while (b)
    	{
    		if (a < b)
    		{
    			int tmp = b;
    			b = a;
    			a = tmp;
    		}
    		int t = b;
    		b = a % b;
    		a = t;
    	}
    	return a;
    }
    
    int main()
    {
    	int fz = 1;
    	int fm = 1;
    	int res;
    	for (int x = 10; x <= 98; x++)
    	{
    		for (int y = x + 1; y <= 99; y++)
    		{
    			int a = x / 10;
    			int b = x % 10;
    			int c = y / 10;
    			int d = y % 10;
    			if ((b - c) == 0 && (y*a == x*d) && (d != 0))
    			{
    				fz *= a;
    				fm *= d;
    			}
    		}
    	}
    	res = fm / gcd(fz, fm);
    	cout << res << endl;
    	system("pause");
    	return 0;
    }
    


  • 相关阅读:
    JavaWeb工程中web.xml基本配置
    json
    理解文档对象模型(3)
    关于经验模态分解的混叠模态(mode mixing)问题
    android ListView SimpleAdapter 带图片
    JAVA的类和对象
    JAVA的循环结构进阶
    JAVA的数组
    JAVA的循环结构
    JAVA的选择结构(二)
  • 原文地址:https://www.cnblogs.com/llguanli/p/6936387.html
Copyright © 2011-2022 走看看