题目链接:https://projecteuler.net/thread=33
对两位数字,求满足一定条件的数的乘积
1 ab/bc=a/c
2 ab/cb=a/c
3 ba/bc=a/c
4 ba/cb=a/c
给的例子是满足1的,而且还只有四个数,求这四个分数的乘积,最简后的分母是什么?
给的例子满足2的,不符合条件
感觉上,应该没有满足3的两位数,
4 应该满足条件的
a范围 1,9
b范围1,9
c范围2,9
还有就是三个数不能相等。
注意需要约分的。
java代码:
package projecteuler31to40;import java.util.Date;class level33{void solve(){int A=1;int B=1;int Z;int M;for(int a=1;a<=9;a++){for(int b=1;b<=9;b++){for(int c=2;c<=9;c++){Z=a*10+b;M=b*10+c;if(Z*c==M*a && a!=b && a<c){A=A*a;B=B*c;System.out.println(Z+"/"+M+" = "+a+"/"+c);}Z=b*10+a;M=c*10+b;if(Z*c==M*a && a!=b && a<c){A=A*a;B=B*c;System.out.println(Z+"/"+M+" = "+a+"/"+c);}}}}System.out.println(A+" "+B);int res=B/gcd(A,B);System.out.println(res);}int gcd(int a,int b){if(a<b){int temp=a;a=b;b=temp;}int r;while(b!=0){r=a%b;a=b;b=r;}return a;}
}
public class Problem33 {public static void main(String[] args){Date beginTime=new Date();new level33().solve();Date endTime=new Date();Long Time=endTime.getTime()-beginTime.getTime();System.out.println("Time="+Time/1000+"秒"+Time%1000+"毫秒");}
}
结果只有下面四个,只是满足1的数:
16/64 = 1/4 19/95 = 1/5 26/65 = 2/5 49/98 = 4/8
Python代码:
def gcd(a,b):if a<b:temp=aa=bb=tempr=0;while b!=0:r=a%ba=bb=rreturn adef solve():res1=1res2=1for a in range(1,10):for b in range(1,10):for c in range(2,10):z=a*10+bm=b*10+cif z*c==m*a and a!=b and a<c:res1=res1*ares2=res2*cprint z,m,a,cprint res2/gcd(res1,res2)