Dada una array de montos y periodo_tiempo que representa la cantidad de dinero que invierten N personas y el periodo de tiempo durante el cual han invertido. La tarea es calcular la relación de beneficio al final.
Ejemplos:
Entrada: n = 2,
Monto1 = 7000, Tiempo1 = 12 meses
Monto2 = 6000, Tiempo2 = 6 meses
Salida: 7 : 3
Entrada: n = 3,
Monto1 = 5000, Tiempo1 = 6 meses
Monto2 = 6000, Tiempo2 = 6 meses
Monto3 = 1000, Tiempo3 = 12 meses
Salida: 5 : 6: 2
Fórmula:
Participación de la 1.ª persona: (Cantidad de dinero invertida por la 1.ª) * (Período de tiempo de la 1.ª) Participación de la
2.ª persona: (Cantidad de dinero invertida por la 2.ª) * (Período de tiempo de la 2.ª) Participación de la
3.ª persona: (Cantidad de dinero invertida por la 3.ª) * (Período de tiempo del 3.°) y así sucesivamente…
Proporción: Participación de la 1.ª persona: Participación de la 2.ª persona: Participación de la 3.ª persona
A continuación se muestra la implementación requerida:
C++
// C++ implementation of above approach #include <bits/stdc++.h> 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
<?php // PHP implementation of // above approach // Recursive function to // return gcd of a and b function __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. 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
<script> // javascript implementation of // above approach // Recursive function to // return gcd of a and b function __gcd(a , 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. function find_Gcd(crr , n) { var i; var 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) { var i; var crr = Array(n).fill(0); for (i = 0; i < n; i++) crr[i] = amountArr[i] * timeTrr[i]; var Share = find_Gcd(crr, n); for (i = 0; i < n - 1; i++) document.write(crr[i] / Share + " : "); document.write(crr[i] / Share); } // Driver Code var amountArr = [ 5000, 6000, 1000 ]; var timeTrr = [ 6, 6, 12 ]; var n = amountArr.length; profitRatio(amountArr, timeTrr, n); // This code contributed by aashish1995 </script>
5 : 6 : 2
Complejidad de tiempo: O (nlogm) donde m es el valor máximo de la array crr.
Espacio Auxiliar: O(n)
Publicación traducida automáticamente
Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA