Dado un entero positivo n, imprima los primeros k dígitos después del punto en valor de 1/n. Su programa debe evitar el desbordamiento y la aritmética de punto flotante.
Ejemplos:
Input: n = 3, k = 3 Output: 333 Input: n = 50, k = 4 Output: 0200
Recomendamos encarecidamente minimizar el navegador y probarlo usted mismo primero.
Consideremos un ejemplo n = 7, k = 3. El primer dígito de 1/7 es ‘1’, se puede obtener haciendo el valor entero de 10/7. El resto de 10/7 es 3. El siguiente dígito es 4, que se puede obtener tomando el valor entero de 30/7. El resto de 30/7 es 2. Los siguientes dígitos son 2, que se pueden obtener tomando el valor entero de 20/7
C++
#include <iostream> using namespace std; // Function to print first k digits after dot in value // of 1/n. n is assumed to be a positive integer. void print(int n, int k) { int rem = 1; // Initialize remainder // Run a loop k times to print k digits for (int i = 0; i < k; i++) { // The next digit can always be obtained as // doing (10*rem)/10 cout << (10 * rem) / n; // Update remainder rem = (10*rem) % n; } } // Driver program to test above function int main() { int n = 7, k = 3; print(n, k); cout << endl; n = 21, k = 4; print(n, k); return 0; }
Java
// Java code to Print first k // digits of 1/n where n is a // positive integer import java.io.*; class GFG { // Function to print first // k digits after dot in value // of 1/n. n is assumed to be // a positive integer. static void print(int n, int k) { // Initialize remainder int rem = 1; // Run a loop k times to print k digits for (int i = 0; i < k; i++) { // The next digit can always be // obtained as doing (10*rem)/10 System.out.print( (10 * rem) / n); // Update remainder rem = (10 * rem) % n; } } // Driver program public static void main(String []args) { int n = 7, k = 3; print(n, k); System.out.println(); n = 21; k = 4; print(n, k); } } // This article is contributed by vt_m
Python3
# Python code to Print first k # digits of 1/n where n is a # positive integer import math # Function to print first k digits # after dot in value of 1/n. n is # assumed to be a positive integer. def Print(n, k): rem = 1 # Initialize remainder # Run a loop k times to print # k digits for i in range(0, k): # The next digit can always # be obtained as doing # (10*rem)/10 print(math.floor(((10 * rem) / n)), end="") # Update remainder rem = (10*rem) % n # Driver program to test # above function n = 7 k = 3 Print(n, k); print(" ") n = 21 k = 4 Print(n, k); # This code is contributed by Sam007.
C#
// C# code to Print first k digits of // 1/n where n is a positive integer using System; class GFG { // Function to print first // k digits after dot in value // of 1/n. n is assumed to be // a positive integer. static void print(int n, int k) { // Initialize remainder int rem = 1; // Run a loop k times to // print k digits for (int i = 0; i < k; i++) { // The next digit can always be // obtained as doing (10*rem)/10 Console.Write( (10 * rem) / n); // Update remainder rem = (10 * rem) % n; } } // Driver program public static void Main() { int n = 7, k = 3; print(n, k); Console.WriteLine(); n = 21; k = 4; print(n, k); } } // This code is contributed by Sam007.
PHP
<?php // Function to print first k digits // after dot in value of 1/n. n is // assumed to be a positive integer. function println($n, $k) { // Initialize remainder $rem = 1; // Run a loop k times // to print k digits for ($i = 0; $i < $k; $i++) { // The next digit can always // be obtained as doing // (10 * rem) / 10 echo floor((10 * $rem) / $n); // Update remainder $rem = (10 * $rem) % $n; } } // Driver Code $n = 7; $k = 3; println($n, $k); echo "\n"; $n = 21; $k = 4; println($n, $k); // This code is contributed by aj_36 ?>
Javascript
<script> // Function to print first k digits after dot in value // of 1/n. n is assumed to be a positive integer. function print(n, k) { let rem = 1; // Initialize remainder let ans = ''; // Run a loop k times to print k digits for (let i = 0; i < k; i++) { // The next digit can always be obtained as // doing (10*rem)/10 ans += Math.floor(((10 * rem) / n)); // Update remainder rem = (10*rem) % n; } document.write(ans) } // Driver program to test above function let n = 7; let k = 3; print(n, k); document.write("<br>"); n = 21; k = 4; print(n, k); </script>
Producción :
142 0476
Complejidad de tiempo: O(k)
Espacio Auxiliar: O(1)
Referencia:
Algoritmos y programación: problemas y soluciones por Alexander Shen
Este artículo es una contribución de Sachin . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA