Nos dan dos números A y B. Necesitamos calcular el número de dígitos después del decimal. Si en caso de que los números sean irracionales, imprima «INF».
Ejemplos:
Input : x = 5, y = 3 Output : INF 5/3 = 1.666.... Input : x = 3, y = 6 Output : 1 3/6 = 0.5
La idea es simple: seguimos la división escolar y hacemos un seguimiento de los residuos mientras dividimos uno por uno. Si el resto se convierte en 0, devolvemos el recuento de dígitos que se ven después del decimal. Si el resto se repite, devolvemos INF.
C++
// CPP program to count digits after dot when a // number is divided by another. #include <bits/stdc++.h> using namespace std; int count(int x, int y) { int ans = 0; // Initialize result unordered_map<int, int> m; // calculating remainder while (x % y != 0) { x = x % y; ans++; // if this remainder appeared before then // the numbers are irrational and would not // converge to a solution the digits after // decimal will be infinite if (m.find(x) != m.end()) return -1; m[x] = 1; x = x * 10; } return ans; } // Driver code int main() { int res = count(1, 2); (res == -1)? cout << "INF" : cout << res; cout << endl; res = count(5, 3); (res == -1)? cout << "INF" : cout << res; cout << endl; res = count(3, 5); (res == -1)? cout << "INF" : cout << res; return 0; }
Java
// Java program to count digits after dot when a // number is divided by another. import java.util.*; class GFG { static int count(int x, int y) { int ans = 0; // Initialize result Map<Integer,Integer> m = new HashMap<>(); // calculating remainder while (x % y != 0) { x = x % y; ans++; // if this remainder appeared before then // the numbers are irrational and would not // converge to a solution the digits after // decimal will be infinite if (m.containsKey(x)) return -1; m.put(x, 1); x = x * 10; } return ans; } // Driver code public static void main(String[] args) { int res = count(1, 2); if((res == -1)) System.out.println("INF"); else System.out.println(res); res = count(5, 3); if((res == -1)) System.out.println("INF"); else System.out.println(res); res = count(3, 5); if((res == -1)) System.out.println("INF"); else System.out.println(res); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to count digits after dot # when a number is divided by another. def count(x, y): ans = 0 # Initialize result m = dict() # calculating remainder while x % y != 0: x %= y ans += 1 # if this remainder appeared before then # the numbers are irrational and would not # converge to a solution the digits after # decimal will be infinite if x in m: return -1 m[x] = 1 x *= 10 return ans # Driver Code if __name__ == "__main__": res = count(1, 2) print("INF") if res == -1 else print(res) res = count(5, 3) print("INF") if res == -1 else print(res) res = count(3, 5) print("INF") if res == -1 else print(res) # This code is contributed by # sanjeev2552
C#
// C# program to count digits after dot when a // number is divided by another. using System; using System.Collections.Generic; class GFG { static int count(int x, int y) { int ans = 0; // Initialize result Dictionary<int,int> m = new Dictionary<int,int>(); // calculating remainder while (x % y != 0) { x = x % y; ans++; // if this remainder appeared before then // the numbers are irrational and would not // converge to a solution the digits after // decimal will be infinite if (m.ContainsKey(x)) return -1; m.Add(x, 1); x = x * 10; } return ans; } // Driver code public static void Main(String[] args) { int res = count(1, 2); if((res == -1)) Console.WriteLine("INF"); else Console.WriteLine(res); res = count(5, 3); if((res == -1)) Console.WriteLine("INF"); else Console.WriteLine(res); res = count(3, 5); if((res == -1)) Console.WriteLine("INF"); else Console.WriteLine(res); } } // This code is contributed by 29AjayKumar
Javascript
<script> // JavaScript program to count digits after dot when a // number is divided by another. function count(x,y) { let ans = 0; // Initialize result let m = new Map(); // calculating remainder while (x % y != 0) { x = x % y; ans++; // if this remainder appeared before then // the numbers are irrational and would not // converge to a solution the digits after // decimal will be infinite if (m.has(x)) return -1; m.set(x, 1); x = x * 10; } return ans; } // Driver code let res = count(1, 2); if((res == -1)) document.write("INF"+"<br>"); else document.write(res+"<br>"); res = count(5, 3); if((res == -1)) document.write("INF"+"<br>"); else document.write(res+"<br>"); res = count(3, 5); if((res == -1)) document.write("INF"+"<br>"); else document.write(res+"<br>"); // This code is contributed by rag2127 </script>
Producción:
1 INF 1
Complejidad de tiempo: O(N * log(N) )
Espacio Auxiliar: O(N)
Este artículo es una contribución de Rahul Chawla . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
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