1081. Rational Sum (20)

题目地址 http://www.patest.cn/contests/pat-a-practise/1081

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

/* 注意数据long int 分子为0的情况, 下面是做的时候提交的ac代码,也是错了几遍,慢慢调试出来的。 发现此题应该是非常简单的 ,要注意细节问题, 这样就能节约更多的时间 */
#include <iostream> 
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

#define N 101

int  n;

long int gcd(long int m, long int n)
{
  if (m < n)
  {
    long int tmp = m;
    m = n;
    n = tmp;
  }
  long int r = m % n;
  while (r != 0)
  {
    m = n;
    n = r;
    r = m % n;
  }
  return n;
}

//long int lcs(long int m, long int n)
//{
// long int sum = m *n;
// if (m < n)
// {
// long int tmp = m;
// m = n;
// n = tmp;
// }
// long int r = m % n;
// while (r != 0)
// {
// m = n;
// n = r;
// r = m % n;
// }
// return sum / n;
//}

void cal(long int a1, long int b1, long int a2, long int b2,long int* at,long int* bt)
{
  if (a1 == 0 && a2 == 0)
  {
    *at = 0;
    *bt = 1;
    return;
  }
  long int fenmu = b1 * b2;
  long int fezhi = a1 * b2 + a2* b1;
  long int flag = 1;
  if (fezhi == 0)
  {
    *at = 0;
    *bt = 1;
    return;
  }
  if (fezhi < 0)
  {
    flag = -1;
    fezhi = -fezhi;
  }
  long int gcdd = gcd(fenmu, fezhi);
  fenmu /= gcdd;
  fezhi /= gcdd;

  *at = flag * fezhi;
  *bt = fenmu;
}

int main()
{
  //freopen("in", "r", stdin);
  long int i;
  long int a1 = 0 , b1 = 1 , a2 = 0, b2 = 1;
  long int tmpa = 0, tmpb = 1;
  while (scanf("%d",&n) != EOF)
  {
    tmpa = 0;
    tmpb = 1;
    for (i = 0; i < n; i++)
    {
      scanf("%ld/%ld", &a2, &b2);
      a1 = tmpa;
      b1 = tmpb;
      cal(a1, b1, a2, b2, &tmpa, &tmpb);
    }

    if (tmpa == 0)
    {
      printf("0");
    }
    else{

      long int flag = 1;
      if (tmpa < 0)
      {
        flag = -1;
        tmpa = -tmpa;
      }
      if (flag == -1)
        printf("-");

      bool flag2 = false;
      if (tmpa >= tmpb)
      {
        printf("%ld", tmpa / tmpb);
        tmpa = tmpa % tmpb;
        flag2 = true;
      }
      if (tmpa != 0)
      {
        if (flag2)
          printf(" %ld/%ld", tmpa, tmpb);
        else
          printf("%ld/%ld", tmpa, tmpb);
      }
    }
    printf("\n");
  }

  return 0;
}

你可能感兴趣的:(数学)