📜  查找数组数字乘积中的最后 k 位数字

📅  最后修改于: 2022-05-13 01:57:49.305000             🧑  作者: Mango

查找数组数字乘积中的最后 k 位数字

给定一个大小为 n 的数组,找出数组数字乘积的最后 k 位 (1 <= k < 10)

例子:

Input  : a[] = {22, 31, 44, 27, 37, 43}
Output : 56

Input  : a[] = {24, 7, 144, 77, 29, 19}
Output : 84

一个简单的解决方案是将所有数字相乘,然后找到产品的最后 k 位数字。此解决方案可能会导致溢出,因为阵列产品可能很高。

更好的解决方案是在 10 k的模下乘以数组元素

C++
// CPP program to find the last k digits in
// product of array
#include 
using namespace std;
 
// Returns last k digits in product of a[]
int lastKDigits(int a[], int n, int k)
{
    int num = (int)pow(10, k);
 
    // Multiplying array elements under
    // modulo 10^k.
    int mul = a[0] % num;
    for (int i = 1; i < n; i++) {
        a[i] = a[i] % num;
        mul = (a[i] * mul) % num;
    }
    return mul;
}
 
// Driven program
int main()
{
    int a[] = { 22, 31, 44, 27, 37, 43 };
    int k = 2;
    int n = sizeof(a) / sizeof(a[0]);
    cout << lastKDigits(a, n, k);
    return 0;
}


Java
// Java program to find
// the last k digits in
// product of array
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Returns last k digits in product of a[]
    static int lastKDigits(int a[], int n, int k)
    {
        int num = (int)(Math.pow(10, k));
     
        // Multiplying array elements
        // under modulo 10^k.
        int mul = a[0] % num;
         
        for (int i = 1; i < n; i++) {
            a[i] = a[i] % num;
            mul = (a[i] * mul) % num;
        }
        return mul;
    }
     
// Driven program
public static void main(String args[])
{
    int a[] = { 22, 31, 44, 27, 37, 43 };
    int k = 2;
    int n = a.length;
     
    System.out.println(lastKDigits(a, n, k));
}
 
}
 
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to find the last
# k digits inproduct of array
import math
 
# Returns last k digits
# in product of a[]
def lastKDigits(a, n, k) :
     
    num = (int)(math.pow(10, k))
     
    # Multiplying array elements
    # under modulo 10^k.
    mul = a[0] % num
     
    for i in range(1,n) :
        a[i] = a[i] % num
        mul = (a[i] * mul) % num
     
    return mul
     
 
# Driven program
a = [ 22, 31, 44, 27, 37, 43 ]
k = 2
n = len(a)
print(lastKDigits(a, n, k))
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find
// the last k digits in
// product of array
using System;
 
class GFG {
     
    // Returns last k digits in product of a[]
    static int lastKDigits(int []a, int n, int k)
    {
        int num = (int)(Math.Pow(10, k));
     
        // Multiplying array elements
        // under modulo 10^k.
        int mul = a[0] % num;
         
        for (int i = 1; i < n; i++) {
            a[i] = a[i] % num;
            mul = (a[i] * mul) % num;
        }
        return mul;
    }
     
    // Driven program
    public static void Main()
    {
        int []a = { 22, 31, 44, 27, 37, 43 };
        int k = 2;
        int n = a.Length;
         
        Console.WriteLine(lastKDigits(a, n, k));
    }
 
}
 
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

56

时间复杂度:O(n)