📜  数字数组乘积中的第一个数字

📅  最后修改于: 2021-05-04 11:04:10             🧑  作者: Mango

给定一个数组“ n”个数字。我们需要找到这些“ n”个数字的乘积的第一位数
例子 :

Input  : arr[] = {5, 8, 3, 7}
Output : 8
        Product of 5, 8, 3, 7 is 840
        and its first  digit is 8

Input  : arr[] = {6, 7, 9}
Output : 3

背景 :
首先,我们从一个非常基本的问题开始,即如何找到任何数字x的第一个数字。要做到这一点,请将数字除以得到大于等于10的数字。完成后,我们将得到的数字将是x的第一个数字。

C++
// C++ implementation to find first digit of a
// single number
#include 
using namespace std;
 
int firstDigit(int x)
{
    // Keep dividing by 10 until it is
    // greater than equal to 10
    while (x >= 10)
        x = x / 10;
    return x;
}
 
// driver function
int main()
{
    cout << firstDigit(12345) << endl;
    cout << firstDigit(5432) << endl;
}


Java
// Java implementation to find first digit of a
// single number
 
class Test {
    static int firstDigit(int x)
    {
        // Keep dividing by 10 until it is
        // greater than equal to 10
        while (x >= 10)
            x = x / 10;
        return x;
    }
 
    // Driver method
    public static void main(String args[])
    {
        System.out.println(firstDigit(12345));
        System.out.println(firstDigit(5432));
    }
}


Python3
# Python implementation to
# find first digit of a
# single number
 
def firstDigit(x):
 
    # Keep dividing by 10 until it is
    # greater than equal to 10
    while(x >= 10):
        x = x//10
    return x
 
  
# driver function
 
print(firstDigit(12345))
print(firstDigit(5432))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to find first
// digit of a single number
using System;
 
public class GFG {
     
    static int firstDigit(int x)
    {
         
        // Keep dividing by 10 until
        // it is greater than equal
        // to 10
        while (x >= 10)
            x = x / 10;
             
        return x;
    }
 
    // Driver method
    public static void Main()
    {
        Console.WriteLine(
                  firstDigit(12345));
                   
        Console.WriteLine(
                   firstDigit(5432));
    }
}
 
// This code is contributed by Sam007.


PHP
= 10)
        $x = $x / 10;
    return floor($x);
}
 
    // Driver Code
    echo firstDigit(12345),"\n" ;
    echo firstDigit(5432) ;
 
// This code is contributed by vishal tripathi.
?>


Javascript


C++
// C++ implementation of finding first digit
// of product of n numbers
#include 
using namespace std;
 
// Teturns the first digit of product of elements of arr[]
int FirstDigit(int arr[], int n)
{
    // stores the logarithm of product of elements of arr[]
    double S = 0;
    for (int i = 0; i < n; i++)
        S = S + log10(arr[i] * 1.0);
 
    // fractional(s) = s - floor(s)
    double fract_S = S - floor(S);
 
    // ans = 10^fract_s
    int ans = pow(10, fract_S);
    return ans;
}
 
// Driver function
int main()
{
    int arr[] = { 5, 8, 3, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << FirstDigit(arr, n) << endl;
    return 0;
}


Java
// Java implementation of finding first digit
// of product of n numbers
 
class Test {
    // Teturns the first digit of product of elements of arr[]
    static int FirstDigit(int arr[], int n)
    {
        // stores the logarithm of product of elements of arr[]
        double S = 0;
        for (int i = 0; i < n; i++)
            S = S + Math.log10(arr[i] * 1.0);
 
        // fractional(s) = s - floor(s)
        double fract_S = S - Math.floor(S);
 
        // ans = 10^fract_s
        int ans = (int)Math.pow(10, fract_S);
        return ans;
    }
 
    // Driver method
    public static void main(String args[])
    {
        int arr[] = { 5, 8, 3, 7 };
 
        System.out.println(FirstDigit(arr, arr.length));
    }
}


Python3
# Python implementation of
# finding first digit
# of product of n numbers
 
import math
 
# Returns the first digit of
# product of elements of arr[]
def FirstDigit (arr, n):
 
    # stores the logarithm of
    # product of elements of arr[]
    S = 0
    for i in range(n):
        S = S + math.log10(arr[i]*1.0)
  
    # fractional(s) = s - floor(s)
    fract_S = S - math.floor(S)
  
    # ans = 10 ^ fract_s
    ans = math.pow(10, fract_S)
    return ans
  
# Driver function
 
arr = [5, 8, 3, 7]
n = len(arr)
print((int)(FirstDigit(arr, n)))
     
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation of finding first
// digit of product of n numbers
using System;
 
public class GFG {
     
    // Teturns the first digit of product
    // of elements of arr[]
    static int FirstDigit(int[] arr, int n)
    {
         
        // stores the logarithm of product
        // of elements of arr[]
        double S = 0;
         
        for (int i = 0; i < n; i++)
            S = S + Math.Log10(arr[i] * 1.0);
 
        // fractional(s) = s - floor(s)
        double fract_S = S - Math.Floor(S);
 
        // ans = 10^fract_s
        int ans = (int)Math.Pow(10, fract_S);
         
        return ans;
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr = { 5, 8, 3, 7 };
        int n = arr.Length;
         
        Console.WriteLine(FirstDigit(arr, n));
    }
}
 
// This code is contributed by Sam007.


PHP


输出:

1
5

解决方案 :
对于数字数组,乘积可能很大,并且它们的乘法可能不适用于任何典型的数据类型。即使您使用Big int,该数字也将非常大,而直接除以10的方法查找第一个数字将非常慢。所以我们需要使用一些不同的东西
让数字成为a_1   a_2   a_3   …… a_n   他们的乘积是P .P = a_1   * a_2   ….. * a_n
令S = log_{10}   (P)= log_{10}   ( a_1   )+ log_{10}   ( a_2   ).. + log_{10}   ( a_n   )。
所以我们可以说P = 10^S
我们知道,任何数字都可以写为其底值和分数值的总和。
因此,P = 10^{floor(S) + fractional (S)}   这意味着P = 10^{floor(S)}   * 10^{fractional(S)}
现在,我们可以应用上面讨论的方法来找到数字的第一个数字,因为在将P除以10直到大于10时,我们只剩下10^{fractional(S)}   这将是我们的答案。分数(S)可以很容易地计算出分数(S)= S –下限(S)。

C++

// C++ implementation of finding first digit
// of product of n numbers
#include 
using namespace std;
 
// Teturns the first digit of product of elements of arr[]
int FirstDigit(int arr[], int n)
{
    // stores the logarithm of product of elements of arr[]
    double S = 0;
    for (int i = 0; i < n; i++)
        S = S + log10(arr[i] * 1.0);
 
    // fractional(s) = s - floor(s)
    double fract_S = S - floor(S);
 
    // ans = 10^fract_s
    int ans = pow(10, fract_S);
    return ans;
}
 
// Driver function
int main()
{
    int arr[] = { 5, 8, 3, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << FirstDigit(arr, n) << endl;
    return 0;
}

Java

// Java implementation of finding first digit
// of product of n numbers
 
class Test {
    // Teturns the first digit of product of elements of arr[]
    static int FirstDigit(int arr[], int n)
    {
        // stores the logarithm of product of elements of arr[]
        double S = 0;
        for (int i = 0; i < n; i++)
            S = S + Math.log10(arr[i] * 1.0);
 
        // fractional(s) = s - floor(s)
        double fract_S = S - Math.floor(S);
 
        // ans = 10^fract_s
        int ans = (int)Math.pow(10, fract_S);
        return ans;
    }
 
    // Driver method
    public static void main(String args[])
    {
        int arr[] = { 5, 8, 3, 7 };
 
        System.out.println(FirstDigit(arr, arr.length));
    }
}

Python3

# Python implementation of
# finding first digit
# of product of n numbers
 
import math
 
# Returns the first digit of
# product of elements of arr[]
def FirstDigit (arr, n):
 
    # stores the logarithm of
    # product of elements of arr[]
    S = 0
    for i in range(n):
        S = S + math.log10(arr[i]*1.0)
  
    # fractional(s) = s - floor(s)
    fract_S = S - math.floor(S)
  
    # ans = 10 ^ fract_s
    ans = math.pow(10, fract_S)
    return ans
  
# Driver function
 
arr = [5, 8, 3, 7]
n = len(arr)
print((int)(FirstDigit(arr, n)))
     
# This code is contributed
# by Anant Agarwal.

C#

// C# implementation of finding first
// digit of product of n numbers
using System;
 
public class GFG {
     
    // Teturns the first digit of product
    // of elements of arr[]
    static int FirstDigit(int[] arr, int n)
    {
         
        // stores the logarithm of product
        // of elements of arr[]
        double S = 0;
         
        for (int i = 0; i < n; i++)
            S = S + Math.Log10(arr[i] * 1.0);
 
        // fractional(s) = s - floor(s)
        double fract_S = S - Math.Floor(S);
 
        // ans = 10^fract_s
        int ans = (int)Math.Pow(10, fract_S);
         
        return ans;
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr = { 5, 8, 3, 7 };
        int n = arr.Length;
         
        Console.WriteLine(FirstDigit(arr, n));
    }
}
 
// This code is contributed by Sam007.

的PHP


输出 :

8