#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> using namespace std; int gcd(int a,int b) { return b == 0 ? a: gcd(b,a%b); } int s[105]; int x[105]; int main() { int n; cin >> n; for(int i = 0;i < n; ++i) { int a,b; scanf("%d/%d",&a,&b); int g = gcd(a,b); s[i] = a/g; x[i] = b/g; } int smallg = 1; for(int i = 0;i < n; ++i) { int g = gcd(smallg,x[i]); smallg = smallg * x[i] / g; } int sum = 0; for(int i = 0;i < n; ++i) { sum += (smallg / x[i])*s[i]; } int g = gcd(sum,smallg); sum = sum / abs(g); smallg = smallg / abs(g); if(sum > 0) { if(sum % smallg == 0) { cout << sum / smallg << endl; } else if(sum / smallg == 0) cout << sum % smallg << "/" << smallg << endl; else cout << sum / smallg << " " << sum % smallg << "/" << smallg << endl; } else { if(sum % smallg == 0) { cout << sum / smallg << endl; } else if(sum / smallg == 0) cout << "-" << (-sum) % smallg << "/" << smallg << endl; else cout << "-"<< (-sum) / smallg << " " << (-sum) % smallg << "/" << smallg << endl; } }
原文地址:https://www.cnblogs.com/Jawen/p/11108426.html
时间: 2024-10-06 10:05:01