求助,橙题RE+WA,感觉自己OI白学了
查看原帖
求助,橙题RE+WA,感觉自己OI白学了
261262
WaltVBAlston楼主2022/10/18 19:30

RT,RE+WA,已知hack数据为: 3 1 1 1 2 1 2 1 1 1

调不出来,求助大佬

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
	if(a%b==0)
		return b;
	else
		return gcd(b,a%b);
}
int nowa,nowb,n;
int main(){
	nowa=0,nowb=1;
	cin>>n;
	while(n--){
		int x,y,k;
		cin>>x>>y>>k;
		if(k==2)
			x*=-1;
		nowa=nowa*y+nowb*x;
		nowb=nowb*y;
		int g=gcd(abs(max(nowa,nowb)),abs(min(nowa,nowb)));
		nowa/=g,nowb/=g;
	}
	if(nowb==1){
		cout<<nowa;
		return 0;
	}
	if(nowb==-1){
		cout<<-nowa;
		return 0;
	}
	if(nowa==0){
		cout<<0;
		return 0;
	}
	if(nowb<0){
		cout<<"-";
		nowb=-nowb;
	}
	cout<<nowa<<"/"<<nowb;
	return 0;
}
2022/10/18 19:30
加载中...