给定三个整数a、b和c,找出a/b中小数点后第一个出现的c。如果不存在,请打印-1。 例如:
null
Input : a = 2 b = 3 c = 6 Output : 1 Explanation: 0.666666.. so 6 occurs at first place of a/b after decimal pointInput : a = 1 b = 4 c = 5 Output : 2 Explanation: 1 / 4 = 0.25 which gives 5's positionto be 2.
A. 幼稚的方法 将执行除法并保留小数部分,迭代并检查给定的数字是否存在。当进行2/3这样的除法时,这将不能很好地工作,因为它产生0.666,但在编程语言中,它会将其四舍五入到0.666667,因此我们也会得到一个7,这在原始a/b中不存在 一 有效的方法 将是数学上的一个,如果我们每次用a乘以b,再乘以10,我们每次都会得到小数点后的整数。所需的调制数为b,因为小数点后最多有b个整数。因此,我们将其与c进行比较,如果它存在,则得到我们想要的值。 以下是上述方法的实施情况:
C++
// CPP program to find first occurrence // of c in a/b #include <bits/stdc++.h> 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
<?php // PHP program to find first // occurrence of c in a/b // function to print // the first digit function first( $a , $b , $c ) { // reduce the number // to its mod $a %= $b ; // traverse for every // decimal places for ( $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 Code $a = 1; $b = 4; $c = 5; echo first( $a , $b , $c ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to find first occurrence // of c in a/b // Function to print the first digit function first(a, b, c) { // Reduce the number to its mod a %= b; // Traverse for every decimal places for (let 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 Code let a = 1, b = 4, c = 5; document.write(first(a, b, c)); // This code is contributed by chinmoy1997pal. </script> |
输出:
2
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END