Dado un número N que representa el número de jugadores que participan en el partido de bádminton. La tarea es determinar el número de partidos necesarios para determinar el ganador. En cada Partido 1 jugador es eliminado.
Ejemplos:
Input: N = 4 Output: Matches played = 3 (As after each match only N - 1 players left) Input: N = 9 Output: Matches played = 8
Planteamiento: Dado que, después de cada partido, un jugador queda eliminado. Entonces, para obtener el ganador, se deben eliminar n-1 jugadores y se deben jugar n-1 partidos.
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 will tell // no. of matches required int noOfMatches(int N) { return N - 1; } // Driver code int main() { int N = 8; cout << "Matches played = " << noOfMatches(N); return 0; }
Java
// Java implementation of above approach import java.io.*; class GFG { // Function that will tell // no. of matches required static int noOfMatches(int N) { return N - 1; } // Driver code public static void main (String[] args) { int N = 8; System.out.println ("Matches played = " + noOfMatches(N)); } } // This code is contributed by jit_t
Python3
# Python 3 implementation of # above approach # Function that will tell # no. of matches required def noOfMatches(N) : return N - 1 # Driver code if __name__ == "__main__" : N = 8 print("Matches played =", noOfMatches(N)) # This code is contributed # by ANKITRAI1
C#
//C# implementation of above approach using System; public class GFG{ // Function that will tell // no. of matches required static int noOfMatches(int N) { return N - 1; } // Driver code static public void Main (){ int N = 8; Console.WriteLine("Matches played = " + noOfMatches(N)); } } // This code is contributed by ajit
PHP
<?php // PHP implementation of above approach // Function that will tell // no. of matches required function noOfMatches($N) { return ($N - 1); } // Driver code $N = 8; echo "Matches played = ", noOfMatches($N); // This code is contributed by akt_mit ?>
Javascript
<script> // Javascript implementation of above approach // Function that will tell // no. of matches required function noOfMatches(N) { return N - 1; } // Driver code var N = 8; document.write("Matches played = " + noOfMatches(N)); // This code is contributed by rutvik_56 </script>
Producción:
Matches played = 7
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