Fraction Addition and Subtraction(592)

592—Fraction Addition and Subtraction

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1. So in this case, 2 should be converted to 2/1.

Example 1:

Input: “-1/2+1/2”
Output: “0/1”

Example 2:

Input:"-1/2+1/2+1/3"
Output: “1/3”

Example 3:

Input:“1/3-1/2”
Output: “-1/6”

Example 4:

Input:“5/3+1/3”
Output: “2/1”

Note:

  1. The input string only contains ‘0’ to ‘9’, ‘/’, ‘+’ and ‘-’. So does the output.
  2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then ‘+’ will be omitted.
  3. The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

C代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int min(int a, int b) {
  if (a > b)
    return b;
  return a;
}

int abs(int a) {
  if (a < 0)
    return -1 * a;
  return a;
}

int getGCD(int a, int b) {  //method:Euclidean GCD algorithm, time complexity:O(log(max(a, b)))
  int r;
  while(b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

char* fractionAddition(char* expression) {

  int a=0, b=0,c=0 ,d=0;
  int sign = 1;          // sign = 1 means '+'; sign = -1 means '-'
  int i = 0;

  if(expression[0] == '-'){
    sign = -1;
    i++;
  }
  while(expression[i] <= '9' && expression[i] >='0') {   //get initial a
    a = a*10 + (expression[i] - '0');
    i++;
  }

  i++; // skip '/'

  a = a * sign;

  while(expression[i] <= '9' && expression[i] >='0') {  //get initial b
    b = b*10+ (expression[i] - '0');
    i++;
  }


  while(expression[i] == '-' || expression[i] == '+') {           //  next c,d exist
    // printf("step into +/- c/d\n");
    c= 0, d=0,sign = 1;
    if(expression[i] == '-'){
      sign = -1;
    }
    i++;
    while(expression[i] <= '9' && expression[i] >='0') {  //get c
      c = c*10+ (expression[i] - '0');
      i++;
    }

    i++;     // skip '/'

    c = c * sign;

    while(expression[i] <= '9' && expression[i] >='0') {  //get d
      d = d*10+ (expression[i] - '0');
      i++;
    }

    /* calculate a/b+c/d = ad+bc/bd */
    a = a*d + b*c;
    b = b*d;

    /* find GCD to simplify a/b */
    int gcd = getGCD(abs(a),abs(b));
    a = a/gcd;
    b = b/gcd;
  }


  if (a == 0) {         //when result = 0
    b = 1;
  }

  /* change a/b into "a/b" */
  static char result[50];
  char strB[50];
  sprintf(result,"%d",a);
  sprintf(strB,"%d",b);
  strcat(result,"/");
  strcat(result,strB);

  return result;
}

Complexity Analysis:

Time complexity : O(nlogx). n is the number of signs(’+’/’-’) in the expression, and Euclidean GCD algorithm: time complexity:O(log(max(a, b))).
Space complexity : O(n).

思路:

  1. 抽象为问题 a/b+ c/d= ad+bc/bd, 关键就是找到a,b,c,d的值.
  2. 找到第一个a,b的值, 然后循环找到c,d值, 进而与前面获得的a,b进行运算, 跟新a,b的值.循环退出的条件是表达式没有运算符了(即后面不存在c,d值了)

注意⚠️:

  • C语言数字转字符串问题, 字符拼接等问题
  • 返回char * 问题, 用static解决

猜你喜欢

转载自blog.csdn.net/kelly_fumiao/article/details/84962571