Escribe un programa para encontrar la proporción en la que un comerciante mezclará dos tipos de arroz por valor de Rs. kg y Rs. kg, por lo que el costo promedio de la mezcla es Rs. kg.
Ejemplos :
Input : X = 50, Y = 70, Z = 65 Output : Ratio = 1:3 Input : X = 1000, Y = 2000, Z = 1400 Output : Ratio = 3:2
De acuerdo con la regla de Alligation , la relación de los pesos de dos elementos mezclados será inversamente proporcional a la desviación de los atributos de estos dos elementos del atributo promedio de la mezcla resultante.
w1 / w2 = (d - m) / (m - c)
El siguiente programa ilustra el enfoque anterior:
C++
#include <bits/stdc++.h> using namespace std; // Function to find the ratio of two mixtures void alligation(float x, float y, float m) { // Find the cheaper among x and y float c = (x <= y) ? x : y; // Find the dearer among x and y float d = (x >= y) ? x : y; // Find ratio r1:r2 int r1 = d - m; int r2 = m - c; // Convert the ration into simpler form int gcd = __gcd(r1, r2); cout << r1 / gcd << ":" << r2 / gcd; } // Driver code int main() { float x, y, z; x = 50; y = 70; z = 65; alligation(x, y, z); return 0; }
Java
// Java implementation of the // above approach. import java.util.*; class solution { static float __gcd(float a, float b) { float dividend,divisor; // a is greater or equal to b if(a>=b) dividend = a; else dividend = b; // b is greater or equal to a if(a<=b) divisor = a; else divisor = b; while(divisor>0) { float remainder = dividend % divisor; dividend = divisor; divisor = remainder; } return dividend; } // Function to find the ratio of two mixtures static void alligation(float x, float y, float m) { // Find the cheaper among x and y float c; if (x <= y) c = x; else c = y; // Find the dearer among x and y float d ; if (x >= y) d = x; else d = y; // Find ratio r1:r2 float r1 = d - m; float r2 = m - c; // Convert the ration into simpler form float gcd = __gcd(r1, r2); System.out.println((int)(r1 / gcd)+":"+(int)(r2 / gcd)); } // Driver code public static void main(String args[]) { float x, y, z; x = 50; y = 70; z = 65; alligation(x, y, z); } } // This code is contributed by // Shashank_sharma
Python3
# Python 3 implementation of the # above approach. from math import gcd # Function to find the ratio # of two mixtures def alligation(x, y, m): # Find the cheaper among x and y if (x <= y): c = x else: c = y # Find the dearer among x and y if (x >= y): d = x else: d = y # Find ratio r1:r2 r1 = d - m r2 = m - c # Convert the ration into simpler form __gcd = gcd(r1, r2) print(r1 // __gcd, ":", r2 // __gcd) # Driver code if __name__ == '__main__': x = 50 y = 70 z = 65 alligation(x, y, z) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the // 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) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find the ratio of // two mixtures static void alligation(float x, float y, float m) { // Find the cheaper among x and y float c = (x <= y) ? x : y; // Find the dearer among x and y float d = (x >= y) ? x : y; // Find ratio r1:r2 int r1 = (int)(d - m); int r2 = (int)(m - c); // Convert the ration into // simpler form int gcd = __gcd(r1, r2); Console.Write(r1 / gcd + ":" + r2 / gcd); } // Driver code public static void Main() { float x, y, z; x = 50; y = 70; z = 65; alligation(x, y, z); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP implementation of the // above approach. function __gcd($a, $b) { $dividend; $divisor; // a is greater or equal to b if($a >= $b) $dividend = $a; else $dividend = $b; // b is greater or equal to a if($a <= $b) $divisor = $a; else $divisor = $b; while($divisor > 0) { $remainder = $dividend % $divisor; $dividend = $divisor; $divisor = $remainder; } return $dividend; } // Function to find the ratio of // two mixtures function alligation($x, $y, $m) { // Find the cheaper among x and y if ($x <= $y) $c = $x; else $c = $y; // Find the dearer among x and y if ($x >= $y) $d = $x; else $d = $y; // Find ratio r1:r2 $r1 = $d - $m; $r2 = $m - $c; // Convert the ration into // simpler form $gcd = __gcd($r1, $r2); echo (int)($r1 / $gcd) . ":" . (int)($r2 / $gcd); } // Driver code $x = 50; $y = 70; $z = 65; alligation($x, $y, $z); // This code is contributed by // Mukul Singh ?>
Javascript
<script> // Javascript implementation of the above approach. // Recursive function to return // gcd of a and b function __gcd(a, b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find the ratio of // two mixtures function alligation(x, y, m) { // Find the cheaper among x and y let c = (x <= y) ? x : y; // Find the dearer among x and y let d = (x >= y) ? x : y; // Find ratio r1:r2 let r1 = (d - m); let r2 = (m - c); // Convert the ration into // simpler form let gcd = __gcd(r1, r2); document.write(parseInt(r1 / gcd, 10) + ":" + parseInt(r2 / gcd, 10)); } let x, y, z; x = 50; y = 70; z = 65; alligation(x, y, z); // This code is contributed by mukesh07. </script>
Producción:
1:3
Publicación traducida automáticamente
Artículo escrito por Smitha Dinesh Semwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA