题解 | #化简分数#

化简分数

https://ac.nowcoder.com/acm/problem/14409

思路

化简就是求一个gcd。

代码

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}

int t,a,b,c,d;

signed main(){
	cin>>t;
	while(t--){
		cin>>a>>b>>c>>d;
		int fm=b*d,fz=a*d+c*b,g=gcd(fm,fz);
		fm/=g;fz/=g;
		cout<<fz<<" "<<fm<<"\n"; 
	}
	return 0;
}
全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务