分数 模板

5 篇文章 0 订阅
5 篇文章 0 订阅

实数即使用double定义也会损失一定精度,在精度要求很高时,就需要用到分数

0.最大公约数&最小公倍数
#define LL long long
LL gcd(LL a,LL b) //最大公约数 
{
	if(b==0) 
		return a;
	return gcd(b,a%b);
}
LL lcm(LL a,LL b) //最小公倍数 
{
	return a*b/gcd(a,b);
}
1.分数存储
struct Fraction //分数 
{
	LL a,b;
	void update() //约分
	{
		LL k=gcd(a,b);
		a/=k; 
		b/=k;
		return;
	}
};
2.“+”
Fraction operator+(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b+x.b*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
3.“ - ”
Fraction operator-(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b-x.b*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
4.“ * ”
Fraction operator*(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
5." / "
Fraction operator/(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b;
	z.b=x.b*y.a;
	z.update();
	return z;
}
6.大小判断
bool operator<(Fraction x,Fraction y)
{
	return x.a*y.b<y.a*x.b;
}
bool operator<=(Fraction x,Fraction y)
{
	return x.a*y.b<=y.a*x.b;
}
bool operator>(Fraction x,Fraction y)
{
	return x.a*y.b>y.a*x.b;
}
bool operator>=(Fraction x,Fraction y)
{
	return x.a*y.b>=y.a*x.b;
}
bool operator==(Fraction x,Fraction y)
{
	return x.a*y.b==y.a*x.b;
}

Code

//比例简化
#include<iostream>
#include<cstdio>
#define LL long long
#define INF 0x3f3f3f3f3f
using namespace std;
const int L=100;
LL gcd(LL a,LL b) //最大公约数 
{
	if(b==0) 
		return a;
	return gcd(b,a%b);
}
LL lcm(LL a,LL b) //最小公倍数 
{
	return a*b/gcd(a,b);
}
struct Fraction //分数 
{
	LL a,b;
	void update()
	{
		LL k=gcd(a,b);
		a/=k; 
		b/=k;
		return;
	}
};
Fraction operator+(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b+x.b*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
Fraction operator-(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b-x.b*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
Fraction operator*(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.a;
	z.b=x.b*y.b;
	z.update();
	return z;
}
Fraction operator/(Fraction x,Fraction y)
{
	Fraction z;
	z.a=x.a*y.b;
	z.b=x.b*y.a;
	z.update();
	return z;
}
bool operator<(Fraction x,Fraction y)
{
	return x.a*y.b<y.a*x.b;
}
bool operator<=(Fraction x,Fraction y)
{
	return x.a*y.b<=y.a*x.b;
}
bool operator>(Fraction x,Fraction y)
{
	return x.a*y.b>y.a*x.b;
}
bool operator>=(Fraction x,Fraction y)
{
	return x.a*y.b>=y.a*x.b;
}
bool operator==(Fraction x,Fraction y)
{
	return x.a*y.b==y.a*x.b;
}
Fraction t;
Fraction x,y;
Fraction ans;
Fraction Min,tmp;
int l;
int main()
{
//	freopen("1.in","r",stdin);
	scanf("%lld%lld",&t.a,&t.b);
	t.update();
	scanf("%d",&l);
	Min.a=INF;
	Min.b=1;
	for(int i=1;i<=l;i++) {
		x.a=i;
		for(int j=1;j<=l;j++) {
			x.b=j;
			if(x<t) continue;
			tmp=x-t;
			if(tmp<Min)
				Min=tmp,ans=x;
		}
	}
	ans.update();
	printf("%lld %lld\n",ans.a,ans.b);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值