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解决
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android resource fraction refers to the ability to specify resource values as fractions or percentages of a parent resource. This is commonly used in Android development to create responsive layouts that adapt to different screen sizes and resolutions. For example, instead of specifying a fixed width or height for a view, you can use resource fractions to define the size relative to the parent container. This allows the view to scale proportionally when the container's size changes. To use resource fractions in Android, you can define them in XML resource files using the "fraction" unit. For instance, you can set the width of a view to half of the parent container by using a fraction value of "0.5". Here's an example of how you can use resource fractions in an XML layout file: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/blue" /> </LinearLayout> ``` In this example, two views are placed inside a LinearLayout with a vertical orientation. Both views have a width defined as half of the parent container, using the "@fraction/0.5" resource value. Using resource fractions can help create more flexible and responsive UI designs in Android applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值