ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#171343 | #957. 分数加法 | heyuzhen | 100 | 1ms | 1220kb | C++ | 485b | 2023-06-22 21:47:32 | 2023-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'