文章目录
前言
一、分数的表示与化简
1.分数的表示
2.分数的化简
二、分数的四则运算
1.分数的加法:
2.分数的减法
3.分数的乘法
4.分数的除法
前言
本文代码模板来自《算法笔记》,书籍如下图所示:
一、分数的表示与化简
1.分数的表示
对于一个分式,我们把它写成假分式的形式
struct Fraction{ //分数 int up, down; //分子,分母 };
我们对于一个分数,制定三项规则:① 保证down(分母)为非负数,如果一个分数是负数,我们要求它的分子为负;② 如果分数为0,令up为0, down为1;③ 分子和分母没有除了1以外的公约数。
2.分数的化简
针对于我们对分数的三种定义,我们在分数的化简时对应的三个步骤:① 如果分母为负数,那么令分子和分母都变成负数;② 如果分子up为0,那么令分母down为1;③ 约分:求出分子的绝对值和分母的绝对值的最大公约数d,然后分子分母同时除以d.
代码为
Fraction reduction (Fraction result) { if (result.down < 0) { result.up = - result.up; result.down = - result.down; } if (result.up == 0) result.down = 1; else { int d = gcd(abs(result.up), abs(result.down)); result.up /= d; result.down /= d; } return result; }
关于gcd函数在这篇博客有详细解释:(先鸽),这里给出代码
int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); }
二、分数的四则运算
1.分数的加法:
对于两个分数f1,f2的加法,计算公式为:
完整代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } struct Fraction { int up, down; }; Fraction reduction (Fraction result) { if (result.down < 0) { result.up = - result.up; result.down = - result.down; } if (result.up == 0) result.down = 1; else { int d = gcd(abs(result.up), abs(result.down)); result.up /= d; result.down /= d; } return result; } Fraction add(Fraction f1, Fraction f2) { Fraction result; result.up = f1.up * f2.down + f2.up * f1.down; result.down = f1.down * f2.down; return reduction(result); } void showResult (Fraction r) { r = reduction(r); if (r.down == 1) printf("%d", r.up); else if (abs(r.up) > r.down) printf("%d %d/%d", r.up / r.down, abs(r.up) % r.down, r.down); else printf("%d/%d", r.up, r.down); } int main() { Fraction f1, f2; scanf("%d%d", &f1.up, &f1.down); scanf("%d%d", &f2.up, &f2.down); Fraction res = add(f1, f2); showResult(res); return 0; }
2.分数的减法
对于两个分数f1,f2的减法,计算公式为:
完整代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } struct Fraction { int up, down; }; Fraction reduction (Fraction result) { if (result.down < 0) { result.up = - result.up; result.down = - result.down; } if (result.up == 0) result.down = 1; else { int d = gcd(abs(result.up), abs(result.down)); result.up /= d; result.down /= d; } return result; } Fraction minu(Fraction f1, Fraction f2) { Fraction result; result.up = f1.up * f2.down - f2.up * f1.down; result.down = f1.down * f2.down; return reduction(result); } void showResult (Fraction r) { r = reduction(r); if (r.down == 1) printf("%d", r.up); else if (abs(r.up) > r.down) printf("%d %d/%d", r.up / r.down, abs(r.up) % r.down, r.down); else printf("%d/%d", r.up, r.down); } int main() { Fraction f1, f2; scanf("%d%d", &f1.up, &f1.down); scanf("%d%d", &f2.up, &f2.down); Fraction res = minu(f1, f2); showResult(res); return 0; }
3.分数的乘法
对于两个分数f1,f2的乘法,计算公式为:
完整代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } struct Fraction { int up, down; }; Fraction reduction (Fraction result) { if (result.down < 0) { result.up = - result.up; result.down = - result.down; } if (result.up == 0) result.down = 1; else { int d = gcd(abs(result.up), abs(result.down)); result.up /= d; result.down /= d; } return result; } Fraction multi(Fraction f1, Fraction f2) { Fraction result; result.up = f1.up * f2.up; result.down = f1.down * f2.down; return reduction(result); } void showResult (Fraction r) { r = reduction(r); if (r.down == 1) printf("%d", r.up); else if (abs(r.up) > r.down) printf("%d %d/%d", r.up / r.down, abs(r.up) % r.down, r.down); else printf("%d/%d", r.up, r.down); } int main() { Fraction f1, f2; scanf("%d%d", &f1.up, &f1.down); scanf("%d%d", &f2.up, &f2.down); Fraction res = multi(f1, f2); showResult(res); return 0; }
4.分数的除法
对于两个分数f1,f2的除法,计算公式为:
完整代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } struct Fraction { int up, down; }; Fraction reduction (Fraction result) { if (result.down < 0) { result.up = - result.up; result.down = - result.down; } if (result.up == 0) result.down = 1; else { int d = gcd(abs(result.up), abs(result.down)); result.up /= d; result.down /= d; } return result; } Fraction divide(Fraction f1, Fraction f2) { Fraction result; result.up = f1.up * f2.down; result.down = f1.down * f2.up; return reduction(result); } void showResult (Fraction r) { r = reduction(r); if (r.down == 1) printf("%d", r.up); else if (abs(r.up) > r.down) printf("%d %d/%d", r.up / r.down, abs(r.up) % r.down, r.down); else printf("%d/%d", r.up, r.down); } int main() { Fraction f1, f2; scanf("%d%d", &f1.up, &f1.down); scanf("%d%d", &f2.up, &f2.down); Fraction res = divide(f1, f2); showResult(res); return 0; }