Fraction Addition and Subtraction
来源:互联网 发布:淘宝申请售后服务培训 编辑:程序博客网 时间:2024/06/08 06:11
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:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has format
±numerator/denominator
. If the first input fraction or the output is positive, then'+'
will be omitted. - 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.
- The number of given fractions will be in the range [1,10].
- The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
// 求两个数的最小公约数// a 和 b 都为正数int com_divisor(int a, int b) { int common_divisor = 1; int half; if (a < b) { half = b / 2; } else if (a > b){ half = a / 2; } else { return a; } int i; for (i = 2; i <= half; ++i) { if (a % i == 0 && b % i == 0) { common_divisor = i; } } return common_divisor;}char* fractionAddition(char* expression) { int len = strlen(expression); int nums = 0; // 分子 int numerator[100]; // 分母 int denumerator[100]; // 游标 int cursor = 0; // 这个数字是正的 int is_positive = -1; // 先判断第一个数字的符号 if (expression[cursor] == '-') { is_positive = 0; ++cursor; } else { is_positive = 1; if (cursor == '+'){ ++cursor; } } // 临时变量 int temp; while(cursor < len) { // 先求分子 temp = 0; while(expression[cursor] != '/'){ temp = temp * 10 + expression[cursor] - 48; ++cursor; } if (!is_positive) { temp = 0 - temp; } numerator[nums] = temp; // 跳过 '/' ++cursor; // 求分母 temp = 0; while(cursor < len && expression[cursor] != '+' && expression[cursor] != '-'){ temp = temp * 10 + expression[cursor] - 48; ++cursor; } denumerator[nums++] = temp; // 判断下一次的分数的符号 if (cursor < len) { if (expression[cursor] == '+') { is_positive = 1; } else { is_positive = 0; } ++cursor; } } // 计算分数的值 int i; int fz = numerator[0]; int fm = denumerator[0]; int common_divisor; for (i = 1; i < nums; ++i) { fz = fz * denumerator[i] + numerator[i] * fm; fm = fm * denumerator[i]; common_divisor = com_divisor(abs(fz), abs(fm)); fz /= common_divisor; fm /= common_divisor; } // 输出 最后的结果 char * res = (char *) malloc(sizeof(char) * 100); if (fz == 0){ res[0] = '0'; res[1] = '/'; res[2] = '1'; res[3] = '\0'; return res; } i = 0; // fz < 0 if (fz < 0) { res[i++] = '-'; fz = abs(fz); } char t_arr[100]; int j = 0; t_arr[j++] = fz % 10 + 48; fz /= 10; while(fz != 0) { t_arr[j++] = fz % 10 + 48; fz /= 10; } while(j > 0) { res[i++] = t_arr[--j]; } res[i++] = '/'; t_arr[j++] = fm % 10 + 48; fm /= 10; while(fm != 0) { t_arr[j++] = fm % 10 + 48; fm /= 10; } while(j > 0) { res[i++] = t_arr[--j]; } res[i] = '\0'; return res; }
阅读全文
0 0
- Fraction Addition and Subtraction
- 592. Fraction Addition and Subtraction
- 592. Fraction Addition and Subtraction
- 592. Fraction Addition and Subtraction
- leetcode 592. Fraction Addition and Subtraction
- [leetcode]592. Fraction Addition and Subtraction
- Fraction Addition and Subtraction问题及解法
- 算法分析与设计第十八周:592. Fraction Addition and Subtraction
- leetcode 592. Fraction Addition and Subtraction C++的stringstream的一个很好的示例
- [arc066e]Addition and Subtraction Hard
- Binary search with addition and subtraction
- Output result string after numbers addition and subtraction
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- UVA 12163 - Addition-Subtraction Game(博弈)
- uva 12163 - Addition-Subtraction Game(组合游戏)
- UVA - 12163 Addition-Subtraction Game (博弈)
- LA 4411 Addition-Subtraction Game(无环图上的博弈,SG)
- uva12163 - Addition-Subtraction Game SG函数 状态压缩
- codeforces768D(87/600)
- 笨办法学 Python · 续 练习 32:扫描器
- Remember-Me功能
- 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN
- Python中 如何将一个字符串分成一个个字符;
- Fraction Addition and Subtraction
- [Leetcode] 352. Data Stream as Disjoint Intervals 解题报告
- 网易技术笔试题整理
- Hdu6108 小C的倍数问题(2017"百度之星"程序设计大赛
- centos6.8安装python3.5.1
- 《深入理解java虚拟机》第一章一点细节
- stm32 输入捕获
- 51nod 1675 序列变换 莫比乌斯反演
- Git 常用操作(入门级)