Summing the number L1-009 N
Summing the number of links subject -L1-009 N
requirements of this problem is very simple, is to compute the number of N and. The trouble is, these numbers are rational molecular / denominator form gives you the output and must also be in the form of rational numbers.
Input format:
input of the first row is given a positive integer N (≤100). Then the format line a1 / b1 a2 / b2 ... N given rational number. Topic to ensure that all the numerator and denominator are within range of a long integer. Further, negative symbol must appear in front of the molecule.
Output format:
its simplest form the digital output and the - write result integer part soon fractional portion with the fractional portion written numerator / denominator, the denominator is less than the required molecules and they have no common factors. If the integer portion of the result is 0, only the output of the fractional part.
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Output Sample 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Output Sample 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24
Problem-solving ideas
simulation
- This question denominator because the data is relatively large, so to open long long
- Need to simulate point adder, do a denominator common denominator between the two fractions and then each operation, simplification, the number n can not take the least common multiple of the denominator, and finally simplified calculation, the denominator or least common multiple may explode long long
- Then special judge to see if the end result can be reduced to integer
- If it is not turned into an integer, continue to determine whether the score is improper fractions into mixed numbers if need be
- Specific operations, see the code
Attach Code
#include<bits/stdc++.h>
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
int main(){
ll n;
scanf("%lld",&n);
ll a,b,c,d;
scanf("%lld/%lld",&a,&b);
for(ll i=1;i<n;i++){
scanf("%lld/%lld",&c,&d);
int gcd=__gcd(b,d);
int lcm=b*d/gcd;
a=a*lcm/b+c*lcm/d;
b=lcm;
int m=__gcd(a,b);
a/=m;
b/=m;
}
if(b<0){
a=-a;
b=-b;
}
a%b==0?printf("%lld\n",a/b):a/b==0?printf("%lld/%lld\n",a,b):printf("%lld %lld/%lld",a/b,a-a/b*b,b);
return 0;
}