Dado el número total de Patas y Cabezas de Conejos y Palomas. La tarea es calcular el número de conejos y palomas.
Ejemplos:
Input: Heads = 200, Legs = 540 Output: Rabbits = 70, Pigeons = 130
Input: Heads = 100, Legs = 300 Output: Rabbits = 50, Pigeons = 50
Deje que el número total. de Cabezas = 200 y Patas = 540.
Sea el número de Cabezas y Patas de Conejos = X y el de Palomas = Y.
así,
X + Y = 200 .. ecuación 1 (nº de cabezas de un conejo y una paloma = n° total de cabezas)
(Como ambos tienen 1 cabeza)
4X + 2Y = 540 … ecuación 2 (n° de patas del conejo y una paloma = n° total de patas)
(El conejo tiene 4 patas y las palomas tienen 2 patas)
Ahora, resolviendo la ecuación 1 y 2 obtenemos,
4X = 540 – 2Y
4X = 540 – 2 * (200 – X)
4X = 540 – 400 + 2X
2X = 140 => X = 70
X = 70 y Y = 130.
por lo tanto, Conejos = 70 y Palomas = 130
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function that calculates Rabbits int countRabbits(int Heads, int Legs) { int count = 0; count = (Legs)-2 * (Heads); count = count / 2; return count; } // Driver code int main() { int Heads = 100, Legs = 300; int Rabbits = countRabbits(Heads, Legs); cout << "Rabbits = " << Rabbits << endl; cout << "Pigeons = " << Heads - Rabbits << endl; return 0; }
Java
// Java implementation of above approach import java.util.*; import java.lang.*; class GFG { // Function that calculates Rabbits static int countRabbits(int Heads, int Legs) { int count = 0; count = (Legs) - 2 * (Heads); count = count / 2; return count; } // Driver code public static void main(String args[]) { int Heads = 100, Legs = 300; int Rabbits = countRabbits(Heads, Legs); System.out.println("Rabbits = " + Rabbits); System.out.println("Pigeons = " + (Heads - Rabbits)); } } // This code is contributed // by Akanksha Rai(Abby_akku)
Python3
# Python 3 implementation of above approach # Function that calculates Rabbits def countRabbits(Heads, Legs): count = 0 count = (Legs) - 2 * (Heads) count = count / 2 return count # Driver code if __name__ == '__main__': Heads = 100 Legs = 300 Rabbits = countRabbits(Heads, Legs) print("Rabbits =", Rabbits) print("Pigeons =", Heads - Rabbits) # This code is contributed # by Surendra_Gangwar
C#
// C# implementation of above approach using System; class GFG { // Function that calculates Rabbits static int countRabbits(int Heads, int Legs) { int count = 0; count = (Legs) - 2 * (Heads); count = count / 2; return count; } // Driver code public static void Main() { int Heads = 100, Legs = 300; int Rabbits = countRabbits(Heads, Legs); Console.WriteLine("Rabbits = " + Rabbits); Console.WriteLine("Pigeons = " + (Heads - Rabbits)); } } // This code is contributed // by Akanksha Rai(Abby_akku)
PHP
<?php // PHP implementation of above approach // Function that calculates Rabbits function countRabbits($Heads, $Legs) { $count = 0; $count = ($Legs) - 2 * ($Heads); $count = (int) $count / 2; return $count; } // Driver code $Heads = 100; $Legs = 300; $Rabbits = countRabbits($Heads, $Legs); echo "Rabbits = " , $Rabbits , "\n"; echo "Pigeons = " , $Heads - $Rabbits, "\n"; // This code is contributed // by Sach_Code ?>
Javascript
<script> // Javascript implementation of above approach // Function that calculates Rabbits function countRabbits(Heads, Legs) { var count = 0; count = (Legs)-2 * (Heads); count = count / 2; return count; } // Driver code var Heads = 100, Legs = 300; var Rabbits = countRabbits(Heads, Legs); document.write( "Rabbits = " + Rabbits + "<br>"); document.write( "Pigeons = " + (Heads - Rabbits)); </script>
Rabbits = 50 Pigeons = 50
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
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