dado personaje C y el un número entero N donde N representa el número de bases en la posición C donde C también puede ser una o cola. Podemos lanzar las monedas N veces, y en la ronda el jugador lanzará la cara de todas las monedas cuyo número sea menor o igual que i. La tarea es determinar el número total de caras y cruces después de voltear N veces posibles.
Ejemplos:
Aporte: C = ‘H’, N = 5
Producción: Cabeza = 2, Cola = 3
Explicación:
Primero H significa que todas las monedas están boca arriba hacia la cabeza, N significa el número total de monedas.
Entonces al principio i = 0, tenemos: HHHHH
Después de la primera ronda eso es i = 1: THHHH
Después de la segunda ronda eso es i = 2: HTHHH
Después de la tercera ronda eso es i = 3: THTHH
Después de la cuarta ronda eso es i = 4: HTHTH
Después de la quinta ronda eso es i = 5: THTHT
Así que el recuento total de cabezas es 2 y la cola es 3.Aporte: C = ‘T’, N = 7
Producción: Cabeza = 4, Cola = 3
Explicación:
Después de cada voltereta posible, el recuento de cara y cruz es 4 y 3.
Acercarse:
Para resolver el problema mencionado anteriormente, debemos seguir los pasos que se detallan a continuación:
- En la pregunta anterior, si observamos, hay un patrón, si primero, todas las monedas van hacia una dirección, entonces el número total de caras después de N rondas será el valor mínimo (n/2) y el valor de la celda cruz (n/2).
- De lo contrario, si todas las monedas miran hacia cola dirección entonces el número total de cruces después de N rondas tendrá un valor mínimo de (n/2) y caras un valor máximo de (n/2).
A continuación se muestra la implementación:
C++
// C++ program to count total heads // and tails after N flips in a coin #include<bits/stdc++.h> using namespace std; // Function to find count of head and tail pair< int , int > count_ht( char s, int N) { // Check if initially all the // coins are facing towards head pair< int , int >p; if (s == 'H' ) { p.first = floor (N / 2.0); p.second = ceil (N / 2.0); } // Check if initially all the coins // are facing towards tail else if (s == 'T' ) { p.first = ceil (N / 2.0); p.second = floor (N / 2.0); } return p; } // Driver code int main() { char C = 'H' ; int N = 5; pair< int , int > p = count_ht(C, N); cout << "Head = " << (p.first) << "n" ; cout << "Tail = " << (p.second) << "n" ; } // This code is contributed by virusbuddah_ |
Java
// Java program to count // total heads and tails // after N flips in a coin import javafx.util.Pair; public class Main { // Function to find count of head and tail public static Pair <Integer, Integer> count_ht( char s, int N) { // Check if initially all the // coins are facing towards head Pair <Integer, Integer> p = new Pair <Integer, Integer> ( 0 , 0 ); if (s == 'H' ) { p = new Pair <Integer, Integer> (( int )Math.floor(N / 2.0 ), ( int )Math.ceil(N / 2.0 )); } // Check if initially all the coins // are facing towards tail else if (s == 'T' ) { p = new Pair <Integer, Integer> (( int )Math.ceil(N / 2.0 ), ( int )Math.floor(N / 2.0 )); } return p; } public static void main(String[] args) { char C = 'H' ; int N = 5 ; Pair <Integer, Integer> p = count_ht(C, N); System.out.println( "Head = " + p.getKey()); System.out.println( "Tail = " + p.getValue()); } } // This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program to Count total heads # and tails after N flips in a coin # Function to find count of head and tail import math def count_ht( s, N ): # Check if initially all the # coins are facing towards head if s = = "H" : h = math.floor( N / 2 ) t = math.ceil( N / 2 ) # Check if initially all the coins # are facing towards tail elif s = = "T" : h = math.ceil( N / 2 ) t = math.floor( N / 2 ) return [h, t] # Driver Code if __name__ = = "__main__" : C = "H" N = 5 l = count_ht(C, n) print ( "Head = " yo[ 0 ]) print ( "Tail = " yo[ 1 ]) |
C#
// C# program to count total heads // and tails after N flips in a coin using System; class GFG{ // Function to find count of head and tail public static Tuple< int , int > count_ht( char s, int N) { // Check if initially all the // coins are facing towards head Tuple< int , int > p = Tuple.Create(0, 0); if (s == 'H' ) { p = Tuple.Create(( int )Math.Floor(N / 2.0), ( int )Math.Ceiling(N / 2.0)); } // Check if initially all the coins // are facing towards tail else if (s == 'T' ) { p = Tuple.Create(( int )Math.Ceiling(N / 2.0), ( int )Math.Floor(N / 2.0)); } return p; } // Driver Code static void Main() { char C = 'H' ; int N = 5; Tuple< int , int > p = count_ht(C, N); Console.WriteLine( "Head = " + p.Item1); Console.WriteLine( "Tail = " + p.Item2); } } // This code is contributed by divyesh072019 |
JavaScript
<script> // JavaScript program to count total heads // and tails after N flips in a coin // Function to find count of head and tail function count_ht(s, N) { // Check if initially all the // coins are facing towards head var p = [0,0]; if (s == 'H' ) { p[0] = Math.floor(N / 2.0); p[1] = Math.ceil(N / 2.0); } // Check if initially all the coins // are facing towards tail else if (s == 'T' ) { p[0] = Math.ceil(N / 2.0); p[1] = Math.floor(N / 2.0); } return p; } // Driver code var C = 'H' ; var N = 5; var p = count_ht(C, N); document.write( "Head = " + (p[0]) + "<br>" ); document.write( "Tail = " + (p[1]) + "<br>" ); </script> |
Head = 2 Tail = 3
Complejidad del tiempo: O(1)
Espacio auxiliar: De 1)