📜  程序计算利润分配率

📅  最后修改于: 2021-04-29 16:44:37             🧑  作者: Mango

给定一系列金额和time_period,它们表示N个人投资的金额和他们投资的时间段。任务是在末尾计算利润率
例子:

公式:

以下是所需的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Calculating GCD of an array.
int find_Gcd(int crr[], int n)
{
    int i;
    int result = crr[0];
    for (i = 1; i < n; i++)
        result = __gcd(crr[i], result);
 
    return result;
}
 
// Function to calculate the Share
void profitRatio(int amountArr[], int timeTrr[],
                 int n)
{
    int i, crr[n];
    for (i = 0; i < n; i++)
        crr[i] = amountArr[i] * timeTrr[i];
 
    int Share = find_Gcd(crr, n);
 
    for (i = 0; i < n - 1; i++)
        cout << crr[i] / Share << " : ";
    cout << crr[i] / Share;
}
 
// Driver Code
int main()
{
    int amountArr[] = { 5000, 6000, 1000 };
    int timeTrr[] = { 6, 6, 12 };
 
    int n = sizeof(amountArr) / sizeof(amountArr[0]);
 
    profitRatio(amountArr, timeTrr, n);
 
    return 0;
}


Java
// Java implementation of
// above approach
import java.io.*;
 
class GFG
{
 
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
    return 0;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
    return __gcd(a, b - a);
}
 
// Calculating GCD of an array.
static int find_Gcd(int crr[], int n)
{
    int i;
    int result = crr[0];
    for (i = 1; i < n; i++)
        result = __gcd(crr[i], result);
 
    return result;
}
 
// Function to calculate the Share
static void profitRatio(int amountArr[],
                        int timeTrr[],
                        int n)
{
    int i;
    int crr[] = new int[n] ;
    for (i = 0; i < n; i++)
        crr[i] = amountArr[i] *
                 timeTrr[i];
 
    int Share = find_Gcd(crr, n);
 
    for (i = 0; i < n - 1; i++)
    System.out.print(crr[i] / Share + " : ");
    System.out.print(crr[i] / Share);
}
 
// Driver Code
public static void main (String[] args)
{
    int amountArr[] = {5000, 6000, 1000};
    int timeTrr[] = {6, 6, 12};
     
    int n = amountArr.length;
     
    profitRatio(amountArr, timeTrr, n);
}
}
 
// This code is contributed
// by inder_verma.


Python3
# Python3 implementation of above approach
 
# Recursive function to
# return gcd of a and b
def __gcd(a, b):
     
    # Everything divides 0
    if(a == 0 or b == 0):
        return 0;
     
    # base case
    if(a == b):
        return a;
     
    # a is greater
    if(a > b):
        return __gcd(a - b, b);
    return __gcd(a, b - a);
 
# Calculating GCD of an array.
def find_Gcd(crr, n):
    result = crr[0];
    for i in range(1, n):
        result = __gcd(crr[i], result);
    return result;
 
# Function to calculate the Share
def profitRatio(amountArr, timeTrr, n):
    i = 0;
    crr = [0] * n;
    for i in range(n):
        crr[i] = amountArr[i] * timeTrr[i];
     
    Share = find_Gcd(crr, n);
     
    for i in range(n - 1):
        print(int(crr[i] / Share),
                     end = " : ");
    print(int(crr[i + 1] / Share));
 
# Driver Code
amountArr = [5000, 6000, 1000];
timeTrr = [6, 6, 12];
 
n = len(amountArr);
 
profitRatio(amountArr, timeTrr, n);
 
# This code is contributed
# by mits


C#
// C# implementation of
// above approach
using System;
 
class GFG
{
 
// Recursive function to
// return gcd of a and b
static int __gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
    return 0;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
    return __gcd(a, b - a);
}
 
// Calculating GCD of an array.
static int find_Gcd(int []crr, int n)
{
    int i;
    int result = crr[0];
    for (i = 1; i < n; i++)
        result = __gcd(crr[i], result);
 
    return result;
}
 
// Function to calculate the Share
static void profitRatio(int []amountArr,
                        int []timeTrr,
                        int n)
{
    int i;
    int []crr = new int[n] ;
    for (i = 0; i < n; i++)
        crr[i] = amountArr[i] *
                timeTrr[i];
 
    int Share = find_Gcd(crr, n);
 
    for (i = 0; i < n - 1; i++)
    Console.Write(crr[i] / Share + " : ");
    Console.Write(crr[i] / Share);
}
 
// Driver Code
public static void Main ()
{
    int []amountArr = {5000, 6000, 1000};
    int []timeTrr = {6, 6, 12};
     
    int n = amountArr.Length;
     
    profitRatio(amountArr, timeTrr, n);
}
}
 
// This code is contributed
// by inder_verma.


PHP
 $b)
        return __gcd($a - $b, $b);
    return __gcd($a, $b - $a);
}
 
// Calculating GCD of an array.
function find_Gcd($crr, $n)
{
    $i;
    $result = $crr[0];
    for ($i = 1; $i < $n; $i++)
        $result = __gcd($crr[$i],
                        $result);
 
    return $result;
}
 
// Function to calculate the Share
function profitRatio($amountArr,
                     $timeTrr, $n)
{
    $i;
    $crr = array();
    for ($i = 0; $i < $n; $i++)
        $crr[$i] = $amountArr[$i] *
                   $timeTrr[$i];
 
    $Share = find_Gcd($crr, $n);
 
    for ($i = 0; $i < $n - 1; $i++)
        echo $crr[$i] / $Share , " : ";
    echo $crr[$i] / $Share;
}
 
// Driver Code
$amountArr = array(5000, 6000, 1000);
$timeTrr = array(6, 6, 12);
 
$n = count($amountArr);
 
profitRatio($amountArr, $timeTrr, $n);
 
// This code is contributed
// by inder_verma
?>


Javascript


输出:
5 : 6 : 2