上小学的时候,小明经常自己发明新算法。一次,老师出的题目是:
1/4 乘以 8/5
小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)
老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼!
对于分子、分母都是 1~9 中的一位数的情况,还有哪些算式可以这样计算呢?
请写出所有不同算式的个数(包括题中举例的)。
显然,交换分子分母后,例如:4/1 乘以 5/8 是满足要求的,这算做不同的算式。
但对于分子分母相同的情况,2/2 乘以 3/3 这样的类型太多了,不在计数之列!
注意:答案是个整数(考虑对称性,肯定是偶数)。请通过浏览器提交。不要书写多余的内容。
分析:思路很简单就是直接枚举,然后定义分数,分数化简,之后直接按题意计算即可。
答案:14
#include <iostream>
#include <vector>
using namespace std;
struct Fen{
int x;
int y;
};
int ans=0;
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
Fen f(Fen fen)
{
int t=gcd(fen.x,fen.y);
fen.x/=t;
fen.y/=t;
return fen;
}
void print(Fen n)
{
Fen s=f(n);
cout<<s.x<<'/'<<s.y<<endl;
}
int main()
{
for(int a=1;a<=9;a++)
for(int b=1;b<=9;b++)
{
if(a==b) continue;
else{
Fen fen1;
fen1.x=a,fen1.y=b;
for(int c=1;c<=9;c++)
for(int d=1;d<=9;d++)
{
if(c==d) continue;
else{
Fen fen2;
fen2.x=c,fen2.y=d;
int v1=a*10+c;
int v2=b*10+d;
Fen fen3;
fen3.x=v1,fen3.y=v2;
fen1=f(fen1);
fen2=f(fen2);
fen3=f(fen3);
Fen fen4;
fen4.x=fen1.x*fen2.x;
fen4.y=fen1.y*fen2.y;
fen4=f(fen4);
if(fen3.x==fen4.x&&fen3.y==fen4.y)
ans++;
}
}
}
}
cout<<ans<<endl;
return 0;
}