📜  乘以最小的数字即可将浮点数转换为自然数

📅  最后修改于: 2021-04-28 16:52:43             🧑  作者: Mango

给定正浮点数n ,任务是找到最小的整数k,以便将k乘以n时,我们得到一个自然数。
例子:

Input : 30.25
Output : 4
30.25 * 4 = 321, there is no number less than 4
which can convert 30.25 into natural number.

Input : 5.5
Output : 2
5.5 * 2 = 11, there is no number less than 2
which can convert 5.5 into natural number.

Input : 5.33
Output : 100

这个想法是将给定的浮点数转换为分数(不一定是简化形式),并找到分子和分母的GCD。例如,如果输入的浮点数为30.25,我们将转换为分数3025/100。通过找到点的位置可以很容易地做到这一点。
最后得到答案,我们将转换分数的分母除以分母和分子的GCD。例如,GCD 3025和100为25。我们将100除以25,得到的答案为4。
以下是此方法的实现:

C++
// C++ program to find the smallest number to multiply
// to convert a floating point number into natural number.
#include
using namespace std;
 
// Finding GCD of two number
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a%b);
}
 
// Returns smallest integer k such that k * str becomes
// natural. str is an input floating point number
int findnum(string &str)
{
    // Find size of string representing a
    // floating point number.
    int n = str.length();
 
    // Below is used to find denominator in
    // fraction form.
    int count_after_dot = 0;
 
    // Used to find value of count_after_dot
    bool dot_seen = false;
 
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++)
    {
        if (str[i] != '.')
        {
            num = num*10 + (str[i] - '0');
            if (dot_seen == true)
                count_after_dot++;
        }
        else
            dot_seen = true;
    }
 
    // If there was no dot, then number
    // is already a natural.
    if (dot_seen == false)
    return 1;
 
    // Find denominator in fraction form. For example,
    // for 30.25, denominator is 100
    int dem = (int)pow(10, count_after_dot);
 
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator. For example, for
    // 30.25, result is 100 / GCD(3025,100) = 100/25 = 4
    return (dem / gcd(num, dem));
}
 
// Driven Program
int main()
{
    string str = "5.125";
    cout << findnum(str) << endl;
    return 0;
}


Java
// Java program to find the smallest number to multiply
// to convert a floating point number into natural number.
class GFG {
  // Finding GCD of two number
  static int gcd(int a, int b) {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Returns smallest integer k such that k * str becomes
  // natural. str is an input floating point number
  static int findnum(String str) {
    // Find size of string representing a
    // floating point number.
    int n = str.length();
 
    // Below is used to find denominator in
    // fraction form.
    int count_after_dot = 0;
 
    // Used to find value of count_after_dot
    boolean dot_seen = false;
 
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++) {
      if (str.charAt(i) != '.') {
        num = num * 10 + (str.charAt(i) - '0');
        if (dot_seen == true)
          count_after_dot++;
      } else
        dot_seen = true;
    }
 
    // If there was no dot, then number
    // is already a natural.
    if (dot_seen == false)
      return 1;
 
    // Find denominator in fraction form. For example,
    // for 30.25, denominator is 100
    int dem = (int)Math.pow(10, count_after_dot);
 
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator. For example, for
    // 30.25, result is 100 / GCD(3025, 100) = 100/25 = 4
    return (dem / gcd(num, dem));
  }
 
  // Driver code
  public static void main(String[] args) {
    String str = "5.125";
    System.out.print(findnum(str));
  }
}
// This code is contributed by Anant Agarwal.


Python
# Python program to find the smallest number to multiply
# to convert a floating point number into natural number.
# Finding GCD of two number
import math
def gcd(a, b):
 
    if (b == 0):
        return a
    return gcd(b, a%b)
  
# Returns smallest integer k such that k * str becomes
# natural. str is an input floating point number
def findnum(str):
     
    # Find size of string representing a
    # floating point number.
    n = len(str)
    # Below is used to find denominator in
    # fraction form.
    count_after_dot = 0
  
    # Used to find value of count_after_dot
    dot_seen = 0
  
    # To find numerator in fraction form of
    # given number. For example, for 30.25,
    # numerator would be 3025.
    num = 0
    for i in range(n):
        if (str[i] != '.'):
            num = num*10 + int(str[i])
            if (dot_seen == 1):
                count_after_dot += 1
        else:
            dot_seen = 1
  
    # If there was no dot, then number
    # is already a natural.
    if (dot_seen == 0):
       return 1
  
    # Find denominator in fraction form. For example,
    # for 30.25, denominator is 100
    dem = int(math.pow(10, count_after_dot))
  
    # Result is denominator divided by
    # GCD-of-numerator-and-denominator. For example, for
    # 30.25, result is 100 / GCD(3025,100) = 100/25 = 4
    return (dem / gcd(num, dem))
  
# Driver Program
 
str = "5.125"
print findnum(str)
 
# Contributed by: Afzal Ansari


C#
// C# program to find the smallest
// number to multiply to convert a
// floating point number into
// natural number.
using System;
 
class GFG {
     
// Finding GCD of two number
static int gcd(int a, int b)
{
    if (b == 0)
    return a;
    return gcd(b, a % b);
}
 
// Returns smallest integer k
// such that k * str becomes
// natural. str is an input
// floating point number
static int findnum(String str)
{
 
    // Find size of string representing
    // a floating point number.
    int n = str.Length;
 
    // Below is used to find denominator
    // in fraction form.
    int count_after_dot = 0;
 
    // Used to find value of count_after_dot
    bool dot_seen = false;
 
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++)
    {
        if (str[i] != '.')
        {
            num = num * 10 + (str[i] - '0');
            if (dot_seen == true)
            count_after_dot++;
        }
        else
            dot_seen = true;
    }
 
    // If there was no dot, then
    // number is already a natural.
    if (dot_seen == false)
    return 1;
 
    // Find denominator in fraction form.
    // For example, for 30.25,
    // denominator is 100
    int dem = (int)Math.Pow(10, count_after_dot);
 
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator.
    // For example, for 30.25, result is
    // 100 / GCD(3025, 100) = 100/25 = 4
    return (dem / gcd(num, dem));
}
 
// Driver code
public static void Main()
{
    String str = "5.125";
    Console.Write(findnum(str));
}
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出:

8