UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#171343#957. 分数加法heyuzhen1001ms1220kbC++485b2023-06-22 21:47:322023-06-22 21:47:33

answer

#include <bits/stdc++.h>
using namespace std;
long long a,b,c,d,x,y;
bool ok;
int main(){
	cin >> a >> b >> c >> d;
	a *= d;
	c *= b;
	x = a + c;
	y = b * d;
	if(x < 0){
		ok = 1;
		x = -x;
	}
	for(long long i = 2;i <= min(x,y);i++){
		while(x %i == 0 &&y % i == 0){
			x /= i;
			y /= i;
		}
	}
	if(ok)cout << "-";
	if(x > y){
		cout << x / y;
		x %= y;
		if(x)printf("+%lld/%lld",x,y);
	}
	else if(x)printf("%lld/%lld",x,y);
	else cout << 0;
	return 0;
}

详细

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

Test #1:

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

input:

2 3 3 4

output:

1+5/12

result:

ok single line: '1+5/12'

Test #2:

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

input:

5 6 2 15

output:

29/30

result:

ok single line: '29/30'

Test #3:

score: 25
Accepted
time: 1ms
memory: 1188kb

input:

2 6 5 15

output:

2/3

result:

ok single line: '2/3'

Test #4:

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

input:

5 4 3 4

output:

2

result:

ok single line: '2'