📜  数组元素乘法中最右边的非零数字

📅  最后修改于: 2021-04-23 18:23:45             🧑  作者: Mango

给定N个非负整数的数组arr [] 。任务是在数组元素的乘积中找到最正确的非零数字。

例子:

方法:

  1. 如果您了解基本数学,那么问题就太简单了。假定您必须找到最右边的正数。现在,如果有2和5,则将一个数字乘以10的倍数。它们将产生一个最后一个数字为0的数字。
  2. 现在,我们可以做的是将每个数组元素除以最短的可除形式,除以5,然后增加出现次数。
  3. 现在,将每个数组元素划分为最短的可除数形式2,并减少这种情况的发生次数。这样,我们在乘法时就不会考虑2和5的乘法。
  4. 在上述两个循环后,如果计数5不为0,则将乘数值设置为1或5。
  5. 现在将每个数组变量相乘并通过将余数乘以10来仅存储最后一位数字

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the rightmost non-zero
// digit in the multiplication
// of the array elements
int rightmostNonZero(int a[], int n)
{
    // To store the count of times 5 can
    // divide the array elements
    int c5 = 0;
  
    // Divide the array elements by 5
    // as much as possible
    for (int i = 0; i < n; i++) {
        while (a[i] > 0 && a[i] % 5 == 0) {
            a[i] /= 5;
            // increase count of 5
            c5++;
        }
    }
  
    // Divide the array elements by
    // 2 as much as possible
    for (int i = 0; i < n; i++) {
        while (c5 && a[i] > 0 && !(a[i] & 1)) {
            a[i] >>= 1;
  
            // Decrease count of 5, because a '2' and
            // a '5' makes a number with last digit '0'
            c5--;
        }
    }
    long long ans = 1;
    for (int i = 0; i < n; i++) {
        ans = (ans * a[i] % 10) % 10;
    }
  
    // If c5 is more than the multiplier
    // should be taken as 5
    if (c5)
        ans = (ans * 5) % 10;
  
    if (ans)
        return ans;
  
    return -1;
}
  
// Driver code
int main()
{
    int a[] = { 7, 42, 11, 64 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << rightmostNonZero(a, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to return the rightmost non-zero
// digit in the multiplication
// of the array elements
static int rightmostNonZero(int a[], int n)
{
    // To store the count of times 5 can
    // divide the array elements
    int c5 = 0;
  
    // Divide the array elements by 5
    // as much as possible
    for (int i = 0; i < n; i++)
    {
        while (a[i] > 0 && a[i] % 5 == 0)
        {
            a[i] /= 5;
              
            // increase count of 5
            c5++;
        }
    }
  
    // Divide the array elements by
    // 2 as much as possible
    for (int i = 0; i < n; i++)
    {
        while (c5 != 0 && a[i] > 0 && 
                         (a[i] & 1) == 0) 
        {
            a[i] >>= 1;
  
            // Decrease count of 5, because a '2' and
            // a '5' makes a number with last digit '0'
            c5--;
        }
    }
      
    int ans = 1;
    for (int i = 0; i < n; i++) 
    {
        ans = (ans * a[i] % 10) % 10;
    }
  
    // If c5 is more than the multiplier
    // should be taken as 5
    if (c5 != 0)
        ans = (ans * 5) % 10;
  
    if (ans != 0)
        return ans;
  
    return -1;
}
  
// Driver code
public static void main(String args[])
{
    int a[] = { 7, 42, 11, 64 };
    int n = a.length;
  
    System.out.println(rightmostNonZero(a, n));
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach
  
# Function to return the rightmost non-zero
# digit in the multiplication
# of the array elements
def rightmostNonZero(a, n):
      
    # To store the count of times 5 can
    # divide the array elements
    c5 = 0
  
    # Divide the array elements by 5
    # as much as possible
    for i in range(n):
        while (a[i] > 0 and a[i] % 5 == 0):
            a[i] //= 5
              
            # increase count of 5
            c5 += 1
  
    # Divide the array elements by
    # 2 as much as possible
    for i in range(n):
        while (c5 and a[i] > 0 and (a[i] & 1) == 0):
            a[i] >>= 1
  
            # Decrease count of 5, because a '2' and
            # a '5' makes a number with last digit '0'
            c5 -= 1
  
    ans = 1
    for i in range(n):
        ans = (ans * a[i] % 10) % 10
  
    # If c5 is more than the multiplier
    # should be taken as 5
    if (c5):
        ans = (ans * 5) % 10
  
    if (ans):
        return ans
  
    return -1
  
# Driver code
a = [7, 42, 11, 64]
n = len(a)
  
print(rightmostNonZero(a, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the rightmost non-zero
// digit in the multiplication
// of the array elements
static int rightmostNonZero(int[] a, int n)
{
      
    // To store the count of times 5 can
    // divide the array elements
    int c5 = 0;
  
    // Divide the array elements by 5
    // as much as possible
    for (int i = 0; i < n; i++)
    {
        while (a[i] > 0 && a[i] % 5 == 0)
        {
            a[i] /= 5;
              
            // increase count of 5
            c5++;
        }
    }
  
    // Divide the array elements by
    // 2 as much as possible
    for (int i = 0; i < n; i++)
    {
        while (c5 != 0 && a[i] > 0 && 
                         (a[i] & 1) == 0) 
        {
            a[i] >>= 1;
  
            // Decrease count of 5, because a '2' and
            // a '5' makes a number with last digit '0'
            c5--;
        }
    }
      
    int ans = 1;
    for (int i = 0; i < n; i++) 
    {
        ans = (ans * a[i] % 10) % 10;
    }
  
    // If c5 is more than the multiplier
    // should be taken as 5
    if (c5 != 0)
        ans = (ans * 5) % 10;
  
    if (ans != 0)
        return ans;
  
    return -1;
}
  
// Driver code
public static void Main()
{
    int[] a = { 7, 42, 11, 64 };
    int n = a.Length;
  
    Console.WriteLine(rightmostNonZero(a, n));
}
}
  
// This code is contributed by
// Code_@Mech


输出:
6