原文:
给定 n 个分数的分子和分母。任务是找到 n 分数的乘积,并以简化形式输出答案。 例:
input : n = 3
num[] = { 1, 2, 5 }
den[] = { 2, 1, 6 }
output : 5/6
product of 1/2 * 2/1 * 5/6 is 10/12.
reduced form of 10/12 is 5/6.
input : n = 4
num[] = { 1, 2, 5, 9 }
den[] = { 2, 1, 6, 27 }
output : 5/18
想法是在一个变量中找到分子的乘积,比如 new_num。现在,在另一个变量中找到分母的乘积,比如 new_den。 现在,要以约简形式求答案,求 new_num 和 new_den 的最大公约数,并用计算出的 gcd 除 new_num 和 new_den。 以下是本办法的实施:
c
// cpp program to find product of n
// fractions in reduced form.
#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);
}
// print the product of n fraction in reduced form.
void productreduce(int n, int num[], int den[])
{
int new_num = 1, new_den = 1;
// finding the product of all n
// numerators and denominators.
for (int i = 0; i < n; i ) {
new_num *= num[i];
new_den *= den[i];
}
// finding gcd of new numerator and
// denominator
int gcd = gcd(new_num, new_den);
// converting into reduced form.
new_num /= gcd;
new_den /= gcd;
cout << new_num << "/" << new_den << endl;
}
// driven program
int main()
{
int n = 3;
int num[] = { 1, 2, 5 };
int den[] = { 2, 1, 6 };
productreduce(n, num, den);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find product of n
// fractions in reduced form.
import java.io.*;
class gfg {
// 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);
}
// print the product of n fraction in
// reduced form.
static void productreduce(int n, int num[],
int den[])
{
int new_num = 1, new_den = 1;
// finding the product of all n
// numerators and denominators.
for (int i = 0; i < n; i ) {
new_num *= num[i];
new_den *= den[i];
}
// finding gcd of new numerator and
// denominator
int gcd = gcd(new_num, new_den);
// converting into reduced form.
new_num /= gcd;
new_den /= gcd;
system.out.println(new_num "/" new_den);
}
// driven program
public static void main (string[] args) {
int n = 3;
int num[] = { 1, 2, 5 };
int den[] = { 2, 1, 6 };
productreduce(n, num, den);
}
}
//this code is contributed by vt_m.
python 3
# python3 program to find
# product of n fractions
# in reduced form.
# function to return
# gcd of a and b
def gcd(a, b):
if (a == 0):
return b;
return gcd(b % a, a);
# print the product of n
# fraction in reduced form.
def productreduce(n, num, den):
new_num = 1;
new_den = 1;
# finding the product
# of all n numerators
# and denominators.
for i in range(n):
new_num = new_num * num[i];
new_den = new_den * den[i];
# finding gcd of
# new numerator
# and denominator
gcd = gcd(new_num, new_den);
# converting into
# reduced form.
new_num = new_num / gcd;
new_den = new_den / gcd;
print(int(new_num), "/",
int(new_den));
# driver code
n = 3;
num = [1, 2, 5];
den = [2, 1, 6];
productreduce(n, num, den);
# this code is contributed
# by mits
c
// c# program to find product of n
// fractions in reduced form.
using system;
class gfg {
// 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);
}
// print the product of n fraction in
// reduced form.
static void productreduce(int n, int []num,
int []den)
{
int new_num = 1, new_den = 1;
// finding the product of all n
// numerators and denominators.
for (int i = 0; i < n; i ) {
new_num *= num[i];
new_den *= den[i];
}
// finding gcd of new numerator and
// denominator
int gcd = gcd(new_num, new_den);
// converting into reduced form.
new_num /= gcd;
new_den /= gcd;
console.writeline(new_num "/" new_den);
}
// driven program
public static void main () {
int n = 3;
int []num = { 1, 2, 5 };
int []den = { 2, 1, 6 };
productreduce(n, num, den);
}
}
//this code is contributed by vt_m.
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
输出:
5/6
如何避免溢出? 上述pg电子试玩链接的解决方案导致大量溢出。我们可以通过首先找到所有记数器和分母的质因数来避免溢出。一旦我们找到了素因子,我们就可以取消常见的素因子。 注意:当要求你以{ p \u q } ^ {-1 } }的形式表示答案时。 对于这些问题,首先将分子和分母以可约形式 p / q 转换,如上所述。然后,求 q 相对于一个质数 m(一般是 10^9 7)的模乘逆。求 q 的模乘逆后,与 p 相乘,取给定素数 m 的模,得到我们需要的输出。 //感谢 提示此情况。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处