📌  相关文章
📜  在给定的分数中第一次出现数字

📅  最后修改于: 2021-06-26 13:18:00             🧑  作者: Mango

给定三个整数a,b和c,在小数点后的a / b中查找c的第一个匹配项。如果不存在,则打印-1。
例子:

Input : a = 2 b = 3 c = 6 
Output : 1 
Explanation: 
0.666666.. so 6 occurs at first place 
of a/b after decimal point

Input : a = 1 b = 4 c = 5 
Output : 2 
Explanation: 
1 / 4 = 0.25 which gives 5's position
to be 2.

一个简单的方法是执行除法并保留小数部分,然后迭代并检查给定的数字是否存在。当完成诸如2/3之类的除法时,这将无法很好地工作,因为它会产生0.666666666,但是在编程语言中,它将把它四舍五入为0.666667,因此我们得到的7在原始a / b中也不存在
一种有效的方法是数学方法,如果我们每次将a乘以b并将其乘以10,我们每次都会得到小数部分后的整数。所需的调制次数为b,因为在小数点后最多有b个整数。因此,我们将其与c进行比较,如果存在我们想要的值。
下面是上述方法的实现:

C++
// CPP program to find first occurrence
// of c in a/b
#include 
using namespace std;
 
// function to print the first digit
int first(int a, int b, int c)
{
    // reduce the number to its mod
    a %= b;
 
    // traverse for every decimal places
    for (int i = 1; i <= b; i++)
    {
        // get every fraction places
        // when (a*10/b)/c
        a = a * 10;
         
        // check if it is equal to
        // the required integer
        if (a / b == c)
            return i;
         
        // mod the number
        a %= b;
    }
    return -1;
}
 
// driver program to test the above function
int main()
{
    int a = 1, b = 4, c = 5;
    cout << first(a, b, c);
    return 0;
}


Java
// Java program to find first occurrence
// of c in a/b
import java.util.*;
import java.lang.*;
 
public class GfG{
    // Function to print the first digit
    public static int first(int a, int b, int c)
    {
        // Reduce the number to its mod
        a %= b;
 
        // Traverse for every decimal places
        for (int i = 1; i <= b; i++)
        {
            // Get every fraction places
            // when (a*10/b)/c
            a = a * 10;
         
            // Check if it is equal to
            // the required integer
            if (a / b == c)
                return i;
         
            // Mod the number
            a %= b;
        }
        return -1;
    }
     
    // Driver function
    public static void main(String argc[]){
        int a = 1, b = 4, c = 5;
        System.out.println(first(a, b, c));
    }
     
}
/* This code is contributed by Sagar Shukla */


Python3
# Python3 program to find first occurrence
# of c in a/b
 
# function to print the first digit
def first( a , b , c ):
 
    # reduce the number to its mod
    a %= b
     
    # traverse for every decimal places
    for i in range(1, b + 1):
 
        # get every fraction places
        # when (a*10/b)/c
        a = a * 10
 
        # check if it is equal to
        # the required integer
        if int(a / b) == c:
            return i
         
        # mod the number
        a %= b
     
    return -1
 
# driver code to test the above function
a = 1
b = 4
c = 5
print(first(a, b, c))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to find first occurrence
// of c in a/b
using System;
 
public class GfG{
     
    // Function to print the first digit
    public static int first(int a, int b, int c)
    {
 
        // Reduce the number to its mod
        a %= b;
 
        // Traverse for every decimal places
        for (int i = 1; i <= b; i++)
        {
 
            // Get every fraction places
            // when (a*10/b)/c
            a = a * 10;
         
            // Check if it is equal to
            // the required integer
            if (a / b == c)
                return i;
         
            // Mod the number
            a %= b;
        }
 
        return -1;
    }
     
    // Driver function
    public static void Main() {
         
        int a = 1, b = 4, c = 5;
         
        Console.WriteLine(first(a, b, c));
    }
}
 
/* This code is contributed by vt_m */


PHP


Javascript


输出:

2