20求助
查看原帖
20求助
457002
szhl0803楼主2022/10/16 14:55

rt,本地所有样例通过,评测20.

#include <bits/stdc++.h>
using namespace std;
struct node{
	int a,b;// a/b
	node(int _a,int _b){
		a=_a;b=_b; 
	}
	node(){}
	void print()
	{
		if(b!=1)
		printf("%d/%d\n",a,b);
		else printf("%d",a);
	}
};
int gcd(int a,int b)
{
	if(b==0)return a;
	else return gcd(b,a%b);
}
node operator+(node x,node y)
{
	// a/b + c/d = ad+cb/cd
	bool f = 0;
	int a=x.a,b=x.b,c=y.a,d=y.b;
	int up = a*d+c*b,down=b*d;
	if(up<0)up*=-1,f=1;
	int z = gcd(up,down);
	up/=z;down/=z;
	if(f)up*=-1;
	return (node){up,down};
}
node operator-(node x,node y)
{
	// a/b + c/d = ad+cb/cd
	bool f = 0;
	int a=x.a,b=x.b,c=y.a,d=y.b;
	int up = a*d-c*b,down=b*d;
	if(up<0)up*=-1,f=1;
	int z = gcd(up,down);
	up/=z;down/=z;
	if(f)up*=-1;
	return (node){up,down};
}

int main()
{
	int n;cin>>n;
	node ans(0,1);
	while(n--)
	{
		int a,b,opt;cin>>a>>b>>opt;
		node mid(a,b);
		if(opt==1)ans=mid+ans;
		if(opt==2)ans=ans-mid;
	}
	ans.print();
	return 0;
} 
2022/10/16 14:55
加载中...