在除以一个数字时,计算小数点后的位数

我们有两个数字A和B。我们需要计算小数点后的位数。如果数字不合理,则打印“INF”。 例如:

null
Input  : x = 5, y = 3Output : INF5/3 = 1.666....Input :  x = 3, y = 6Output : 13/6 = 0.5 

这个想法很简单,我们遵循学校划分,在逐个划分的同时跟踪剩余部分。如果余数变为0,则返回小数点后的数字计数。如果余数重复,我们返回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>


输出:

1INF1

时间复杂性: O(N*log(N))

辅助空间: O(N)

本文由 拉胡尔·查拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享