UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186815#957. 分数加法chenziju20231000ms1196kbC++11384b2023-10-01 13:11:472023-10-01 13:11:49

answer

#include <bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f;
int main(){
	cin>>a>>b>>c>>d;
	e=__gcd(b,d);
	a=a*d/e+c*b/e;
	b=b*d/e;
	c=0;
	d=0;
	if(a<0){
		cout<<'-';
		c=1;
	}
	if(a/b>0){
		cout<<a/b;
		d=1;
	}
	if(a%b!=0){
		if(d==1)
			cout<<'+';
		if(c==1)
			cout<<'-';
		a=a%b;
		f=__gcd(a,b);
		b=b/f;
		a=a/f;
		cout<<a<<'/'<<b;
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 25
Accepted
time: 0ms
memory: 1196kb

input:

2 3 3 4

output:

1+5/12

result:

ok single line: '1+5/12'

Test #2:

score: 25
Accepted
time: 0ms
memory: 1196kb

input:

5 6 2 15

output:

29/30

result:

ok single line: '29/30'

Test #3:

score: 25
Accepted
time: 0ms
memory: 1196kb

input:

2 6 5 15

output:

2/3

result:

ok single line: '2/3'

Test #4:

score: 25
Accepted
time: 0ms
memory: 1196kb

input:

5 4 3 4

output:

2

result:

ok single line: '2'