📜  计算数组乘积中尾随零的数量

📅  最后修改于: 2021-04-29 12:01:49             🧑  作者: Mango

给定一个数组大小n,我们需要找到数组乘积中的零总数。

例子:

Input  : a[] = {100, 20, 40, 25, 4} 
Output : 6
Product is 100 * 20 * 40 * 25 * 4
which is 8000000 and has 6 trailing 0s.

Input  : a[] = {10, 100, 20, 30, 25, 4, 
                43, 25, 50, 90, 12, 80}
Output : 13

一个简单的解决方案是简单地乘以并计算乘积中的尾随0。此解决方案可能会导致整数溢出。更好的解决方案基于以下事实:零是由2和5的组合形成的。因此,零的数量将取决于可以形成的2和5的对数。
例如:8 * 3 * 5 * 23 * 17 * 25 * 4 * 11
2 3 * 3 1 * 5 1 * 23 1 * 17 1 * 5 2 * 2 2 * 11 1
在此示例中,有5个2和3个5。因此,我们将只能形成3对(2 * 5)。因此,乘积中将为3个零。

C++
// CPP program for count total zero in product of array
#include 
using namespace std;
  
// Returns count of zeros in product of array
int countZeros(int a[], int n)
{
    int count2 = 0, count5 = 0;
    for (int i = 0; i < n; i++) {
  
        // count number of 2s in each element
        while (a[i] % 2 == 0) {
            a[i] = a[i] / 2;
            count2++;
        }
  
        // count number of 5s in each element
        while (a[i] % 5 == 0) {
            a[i] = a[i] / 5;
            count5++;
        }
    }
    // return the minimum
    return (count2 < count5) ? count2 : count5;
}
  
// Driven Program
int main()
{
    int a[] = { 10, 100, 20, 30, 50, 90, 12, 80 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << countZeroso(a, n);
    return 0;
}


Java
// Java program for count total
// zero in product of array
import java.util.*;
import java.lang.*;
  
public class GfG
{
    // Returns count of zeros in product of array
    public static int countZeroso(int[] a, int n)
    {
        int count2 = 0, count5 = 0;
        for (int i = 0; i < n; i++) 
        {
  
            // count number of 2s 
            // in each element
            while (a[i] % 2 == 0) 
            {
                a[i] = a[i] / 2;
                count2++;
            }
  
            // count number of 5s 
            // in each element
            while (a[i] % 5 == 0) 
            {
                a[i] = a[i] / 5;
                count5++;
            }
        }
        // return the minimum
        return (count2 < count5) ? count2 : count5;
    }
      
    // Driver function 
    public static void main(String argc[])
    {
        int[] a = new int[]{ 10, 100, 20, 30, 
                            50, 91, 12, 80 };
        int n = 8;
        System.out.println(countZeroso(a, n));
    }
      
}
  
// This code is contributed 
// by Sagar Shukla


Python 3
# Python 3 program for count 
# total zero in product of array
  
# Returns count of zeros 
# in product of array
def countZeros(a, n) :
    count2 = 0
    count5 = 0
    for i in range(0, n) :
          
        # count number of 2s 
        # in each element
        while (a[i] % 2 == 0) :
            a[i] = a[i] // 2
            count2 = count2 + 1
          
          
        # count number of 5s 
        # in each element
        while (a[i] % 5 == 0) :
            a[i] = a[i] // 5
            count5 = count5 + 1
          
          
    # return the minimum
    if(count2 < count5) :
        return count2
    else : 
        return count5
          
  
# Driven Program
a = [ 10, 100, 20, 30, 50, 90, 12, 80 ]
n = len(a)
print(countZeros(a, n))
  
# This code is contributed 
# by Nikita Tiwari.


C#
// C# program for count total
// zero in product of array
using System;
  
public class GfG
{
    // Returns count of zeros in product of array
    public static int countZeroso(int[] a, int n)
    {
        int count2 = 0, count5 = 0;
        for (int i = 0; i < n; i++) 
        {
  
            // count number of 2s 
            // in each element
            while (a[i] % 2 == 0) 
            {
                a[i] = a[i] / 2;
                count2++;
            }
  
            // count number of 5s 
            // in each element
            while (a[i] % 5 == 0) 
            {
                a[i] = a[i] / 5;
                count5++;
            }
        }
        // return the minimum
        return (count2 < count5) ? count2 : count5;
    }
      
    // Driver function 
    public static void Main()
    {
        int[] a = new int[]{ 10, 100, 20, 30, 
                            50, 91, 12, 80 };
        int n = 8;
        Console.WriteLine(countZeroso(a, n));
    }
      
}
  
// This code is contributed 
// by vt_m


PHP


输出:

9