Dado un número N, la tarea es encontrar el número de ceros en la representación base K del número dado, donde K > 1 .
Ejemplos:
Entrada: N = 10, K = 3
Salida: 1
Explicación: La representación de base 3 de 10 es 101.
Por lo tanto, el número de 0 en 101 es 1.Entrada: N = 8, K = 2
Salida: 3
Explicación: La representación de base 2 de 8 es Por lo tanto, el número de 0 es 3.
Enfoque: este problema se puede resolver convirtiendo el número a la forma base K.
El pseudocódigo para convertir un número N de base 10 a base K :
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to count the number of zeros // in K base representation of N int countZero(int N, int K) { // Making a variable to store count int count = 0; // Looping till n becomes 0 while (N > 0) { // Check if the current digit // is 0 or not. if (N % K == 0) count++; N /= K; } return count; } // Driver code int main() { int N = 8; int K = 2; // Function call cout << countZero(N, K) << endl; return 0; } // This code is contributed by Ashish Kumar
C
// C code to implement the approach #include<stdio.h> // Function to count the number of zeros // in K base representation of N int countZero(int N, int K) { // Making a variable to store count int count = 0; // Looping till n becomes 0 while (N > 0) { // Check if the current digit // is 0 or not. if (N % K == 0) count++; N /= K; } return count; } // Driver code int main() { int N = 8; int K = 2; // Function call int ans = countZero(N,K); printf("%d",ans); return 0; } // This code is contributed by ashishsingh13122000.
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to count the number of zeros // in K base representation of N public static int countZero(int N, int K) { // Making a variable to store count int count = 0; // Looping till n becomes 0 while (N > 0) { // Check if the current digit // is 0 or not. if (N % K == 0) count++; N /= K; } return count; } // Driver Code public static void main(String[] args) { int N = 8; int K = 2; // Function call System.out.println(countZero(N, K)); } } // This code is contributed by Rohit Pradhan
Python3
# Python code to implement the approach # Function to count the number of zeros # in K base representation of N def countZero(N, K): # Making a variable to store count count = 0 # Looping till n becomes 0 while (N > 0): # Check if the current digit # is 0 or not. if (N % K == 0): count += 1 N //= K return count # Driver code N = 8 K = 2 # Function call print(countZero(N, K)) # This code is contributed by shinjanpatra
C#
// C# code to implement the approach using System; class GFG { // Function to count the number of zeros // in K base representation of N static int countZero(int N, int K) { // Making a variable to store count int count = 0; // Looping till n becomes 0 while (N > 0) { // Check if the current digit // is 0 or not. if (N % K == 0) count++; N /= K; } return count; } // Driver Code public static void Main() { int N = 8; int K = 2; // Function call Console.Write(countZero(N, K)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to count the number of zeros // in K base representation of N function countZero(N, K) { // Making a variable to store count let count = 0; // Looping till n becomes 0 while (N > 0) { // Check if the current digit // is 0 or not. if (N % K == 0) count++; N = Math.floor(N / K); } return count; } // Driver code let N = 8; let K = 2; // Function call document.write(countZero(N, K) + '<br>'); // This code is contributed by Potta Lokesh </script>
Producción
3
Complejidad temporal: O(1)
Espacio auxiliar:
Publicación traducida automáticamente
Artículo escrito por chandramauliguptach y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA