Given N rational numbers in the form numerator/denominator
, you are supposed to calculate their sum.
Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 ...
where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.
Output Specification:
For each test case, output the sum in the simplest form integer numerator/denominator
where integer
is the integer part of the sum, numerator
< denominator
, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24
思路
- 分数化简的思路——求分母和分子的最大公约数,同时÷最大公约数
- 分数相加的思路——求两个分母的最大公倍数,分子乘以对应的因子,相加即可
- 设(a,b)的最大公约数为(d),有性质:(a*b/d = 最大公倍数)
代码
#include<bits/stdc++.h>
using namespace std;
long int a[101];
long int b[101];
long int gcd(long int a, long int b)
{
if(b == 0) return a;
else return gcd(b, a%b);
} //求最大公约数
void reduction(long int a[], long int b[], int i)
{
int d = gcd(abs(a[i]), abs(b[i]));
a[i] /= d;
b[i] /= d;
} //根据最大公约数化简分子分母
int main()
{
int n;
scanf("%d", &n);
for(int i=1;i<=n;i++)
{
scanf("%ld/%ld", &a[i], &b[i]);
if(a[i] == 0) b[i] = 1; //为了不让求最大公约数出错,让实际为0的分数的分母弄为1——1和任何数的最大公约数都是1
reduction(a, b, i);
}
a[0] = 0; b[0] = 1; //对第i位的处理是让第i位和第i-1位来求和,所以让第0位表示为0
for(int i=1;i<=n;i++)
{
long int gcd_value = gcd(b[i], b[i-1]);
long int lcm_value = b[i] / gcd_value * b[i-1]; //先/后*减少溢出可能
a[i-1] *= lcm_value / b[i-1];
a[i] *= lcm_value /b[i];
a[i] = a[i] + a[i-1];
b[i] = lcm_value;
reduction(a,b,i);
}
if(abs(a[n]) >= abs(b[n])) //这里要用绝对值是因为会有负数的情况出现,比如-1/1
{
int t = a[n] / b[n];
int up = a[n] - t * b[n];
if(up == 0)
printf("%ld
", t);
else
printf("%ld %ld/%ld
", t, up, b[n]);
}else
{
if(a[n] == 0)
printf("0
"); //这里不单独处理的话,是会输出0/n这种类型的数字的
else
printf("%ld/%ld
", a[n], b[n]);
}
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880