分数是两个值的比率。分数的形式为a / b,其中a称为分子,b称为分母,b不能等于0(因为未定义除以0)。分母给出了相等的部分。分子代表其中有多少个。例如,一半,八分之五,四分之三(1 / 2、8 / 5、3 / 4)。
关于分数的事实:
- 如果分子和分母的最大公约数(gcd)大于1,则可以减小分数。
- 分数的加法和减法:当分数相加或相减时,它们必须具有相同的分母。如果它们没有相同的分母,则我们必须为两者找到一个共同的分母。为此,我们首先需要找到两个分母的最低公倍数(lcm)或将每个分数乘以适当的整数,以便有相同的分母。
- 分数的乘法和除法:将两个分数相乘时,只需将两个分子乘以两个分母即可。当将两个分数相除时,第一个分数必须与第二个分数的倒数相乘。
- 分数分为三种:
- 正确的分数:分子小于分母。例如1 / 3、3 / 4、2 / 7
- 分数不正确:分子大于(或等于)分母。例如4 / 3、11 / 4、7 / 7。
- 混合分数:整数和适当的分数在一起。例如1 1 / 3、2 1 / 4、16 2/5。
如何添加两个分数?
将两个分数a / b和c / d相加,并以最简单的形式打印答案。
例子 :
Input: 1/2 + 3/2
Output: 2/1
Input: 1/3 + 3/9
Output: 2/3
Input: 1/5 + 3/15
Output: 2/5
加两个分数的算法
- 通过找到两个分母的LCM(最小公倍数)来找到一个公分母。
- 更改分数以具有相同的分母并添加两个术语。
- 通过将分子和分母除以最大的公因子,将获得的最终分数简化为更简单的形式。
C++
// C++ program to add 2 fractions
#include
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to convert the obtained fraction
// into it's simplest form
void lowest(int& den3, int& num3)
{
// Finding gcd of both terms
int common_factor = gcd(num3, den3);
// Converting both terms into simpler
// terms by dividing them by common factor
den3 = den3 / common_factor;
num3 = num3 / common_factor;
}
// Function to add two fractions
void addFraction(int num1, int den1, int num2,
int den2, int& num3, int& den3)
{
// Finding gcd of den1 and den2
den3 = gcd(den1, den2);
// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den3 = (den1 * den2) / den3;
// Changing the fractions to have same denominator
// Numerator of the final fraction obtained
num3 = (num1) * (den3 / den1) + (num2) * (den3 / den2);
// Calling function to convert final fraction
// into it's simplest form
lowest(den3, num3);
}
// Driver program
int main()
{
int num1 = 1, den1 = 500, num2 = 2, den2 = 1500, den3, num3;
addFraction(num1, den1, num2, den2, num3, den3);
printf("%d/%d + %d/%d is equal to %d/%d\n", num1, den1,
num2, den2, num3, den3);
return 0;
}
Java
// Java program to add 2 fractions
import java.util.*;
class GFG
{
static int den3, num3;
// Function to return gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to convert the obtained fraction
// into it's simplest form
static void lowest()
{
// Finding gcd of both terms
int common_factor = gcd(num3, den3);
// Converting both terms into simpler
// terms by dividing them by common factor
den3 = den3 / common_factor;
num3 = num3 / common_factor;
}
// Function to add two fractions
static void addFraction(int num1, int den1,
int num2, int den2)
{
// Finding gcd of den1 and den2
den3 = gcd(den1, den2);
// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den3 = (den1 * den2) / den3;
// Changing the fractions to have
// same denominator.
// Numerator of the final fraction obtained
num3 = (num1) * (den3 / den1) +
(num2) * (den3 / den2);
// Calling function to convert final fraction
// into it's simplest form
lowest();
}
// Driver Code
public static void main(String[] args)
{
int num1 = 1, den1 = 500,
num2 = 2, den2 = 1500;
addFraction(num1, den1, num2, den2);
System.out.printf("%d/%d + %d/%d is equal to %d/%d\n",
num1, den1, num2, den2, num3, den3);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to add 2 fractions
# Function to return gcd of a and b
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
# Function to convert the obtained
# fraction into it's simplest form
def lowest(den3, num3):
# Finding gcd of both terms
common_factor = gcd(num3, den3)
# Converting both terms
# into simpler terms by
# dividing them by common factor
den3 = int(den3 / common_factor)
num3 = int(num3 / common_factor)
print(num3, "/", den3)
# Function to add two fractions
def addFraction(num1, den1, num2, den2):
# Finding gcd of den1 and den2
den3 = gcd(den1, den2)
# Denominator of final
# fraction obtained finding
# LCM of den1 and den2
# LCM * GCD = a * b
den3 = (den1 * den2) / den3
# Changing the fractions to
# have same denominator Numerator
# of the final fraction obtained
num3 = ((num1) * (den3 / den1) +
(num2) * (den3 / den2))
# Calling function to convert
# final fraction into it's
# simplest form
lowest(den3, num3)
# Driver Code
num1 = 1; den1 = 500
num2 = 2; den2 = 1500
print(num1, "/", den1, " + ", num2, "/",
den2, " is equal to ", end = "")
addFraction(num1, den1, num2, den2)
C#
// C# program to add 2 fractions
using System;
class GFG
{
static int den3, num3;
// Function to return gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to convert the obtained fraction
// into it's simplest form
static void lowest()
{
// Finding gcd of both terms
int common_factor = gcd(num3, den3);
// Converting both terms into simpler
// terms by dividing them by common factor
den3 = den3 / common_factor;
num3 = num3 / common_factor;
}
// Function to add two fractions
static void addFraction(int num1, int den1,
int num2, int den2)
{
// Finding gcd of den1 and den2
den3 = gcd(den1, den2);
// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den3 = (den1 * den2) / den3;
// Changing the fractions to have
// same denominator.
// Numerator of the final fraction obtained
num3 = (num1) * (den3 / den1) +
(num2) * (den3 / den2);
// Calling function to convert final fraction
// into it's simplest form
lowest();
}
// Driver Code
public static void Main(String[] args)
{
int num1 = 1, den1 = 500,
num2 = 2, den2 = 1500;
addFraction(num1, den1, num2, den2);
Console.Write("{0}/{1} + {2}/{3} is equal to {4}/{5}\n",
num1, den1, num2, den2, num3, den3);
}
}
// This code is contributed by PrinciRaj1992
PHP
输出 :
1/500 + 2/1500 is equal to 1/300
与分数有关的更多问题:
- 馏分的LCM和HCF
- 以字符串格式表示两个数字的分数
- 程序比较两个分数
- 将二进制分数转换为十进制
- 将小数转换为二进制数
- 小背包问题
- 查找分数中的重复序列
最近关于分数的文章!