zoukankan      html  css  js  c++  java
  • (Problem 33)Digit canceling 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<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    void swap(int *a, int *b)
    {
    	int t;
    	t=*a;
    	*a=*b;
    	*b=t;
    }
    
    int gcd(int a, int b)
    {
    	int r;
    	if (a < b)
    		swap(&a,&b);
    	if (!b)
    		return a;
    	while ((r = a % b) != 0) {
    		a = b;
    		b = r;
    	}
    	return b;
    }
    
    void find()
    {
    	int i;
    	int M,N;
    	M=N=1;
    	for(i=12; i<50; i++)
    	{
    		for(int j=i+1; j<100; j++)
    		{
    			int t=gcd(i,j);
    			if(t==1 || i/t>10 || j/t>10 || i%10!=j/10)
    				continue;
    			else
    			{
    				int a=i/10,b=j%10;
    				if(a/gcd(a,b)==i/t && b/gcd(a,b)==j/t)
    				{
    					M*=i/t;
    					N*=j/t;
    				}
    			}
    		}
    	}
    	printf("%d\n",N/gcd(M,N));
    }
    
    int main()
    {
    	find();
    	return 0;
    }
     

    Answer:
    100

  • 相关阅读:
    iOS开发- 蓝牙后台接收数据(BLE4.0)
    代码优化之减少重复代码-实践
    微信iOS多设备多字体适配方案总结
    iOS微信小视频优化心得
    iOS项目工程及目录结构
    手机淘宝 521 性能优化项目揭秘
    最大连续和
    struts2入门
    Maven环境搭配及继承
    easyui高级控件
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367352.html
Copyright © 2011-2022 走看看