📜  检查表示为数组的大数是否可以被 Y 整除

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

检查表示为数组的大数是否可以被 Y 整除

给定一个表示为数组arr[]的大整数X ,其中每个arr[i]X中存储一个数字。任务是检查数组表示的数字是否可以被给定整数Y整除。
例子:

方法:从左边开始遍历给定数字的数字,取小于或等于Y的最大数字除以Y 。如果余数不是0 ,那么它将被传送到由剩余数字形成的下一个可能的数字,就像在长除法中一样。处理完完整的数字后,如果余数仍然不是 0,则表示的数字不能被Y整除,否则它是。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if the number represented
// by the given array is divisible by y
bool isDivisible(int* arr, int n, int y)
{
    int d = 0, i = 0;
 
    // While there are digits left
    while (i < n) {
 
        // Select the next part of the number
        // i.e. the maximum number which is <= y
        while (d < y && i < n)
            d = d * 10 + arr[i++];
 
        // Get the current remainder
        d = d % y;
    }
 
    // If the final remainder is 0
    if (d == 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 1, 5, 6 };
    int x = sizeof(arr) / sizeof(int);
    int y = 4;
 
    cout << (isDivisible(arr, x, y) ? "Yes" : "No");
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function that returns true if the number represented
    // by the given array is divisible by y
    static boolean isDivisible(int [] arr, int n, int y)
    {
        int d = 0, i = 0;
     
        // While there are digits left
        while (i < n)
        {
     
            // Select the next part of the number
            // i.e. the maximum number which is <= y
            while (d < y && i < n)
                d = d * 10 + arr[i++];
     
            // Get the current remainder
            d = d % y;
        }
     
        // If the final remainder is 0
        if (d == 0)
            return true;
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        int [] arr = { 1, 2, 1, 5, 6 };
        int x = arr.length;
        int y = 4;
     
        System.out.println(isDivisible(arr, x, y) ? "Yes" : "No");
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function that returns true if the number represented
# by the given array is divisible by y
def isDivisible(arr, n, y):
    d, i = 0, 0
     
    # While there are digits left
    while i < n:
         
        # Select the next part of the number
        # i.e. the maximum number which is <= y
        while d < y and i < n:
            d = d * 10 + arr[i]
            i += 1
         
        # Get the current remainder
        d = d % y
         
    # If the final remainder is 0
    if d == 0:
        return True
    return False
 
# Driver code
if __name__ == "__main__":
    arr = [ 1, 2, 1, 5, 6 ]
    x = len(arr)
    y = 4
    if (isDivisible(arr, x, y)):
        print("Yes")
    else:
        print("No")
     
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function that returns true if the number represented
    // by the given array is divisible by y
    static bool isDivisible(int [] arr, int n, int y)
    {
        int d = 0, i = 0;
     
        // While there are digits left
        while (i < n)
        {
     
            // Select the next part of the number
            // i.e. the maximum number which is <= y
            while (d < y && i < n)
                d = d * 10 + arr[i++];
     
            // Get the current remainder
            d = d % y;
        }
     
        // If the final remainder is 0
        if (d == 0)
            return true;
        return false;
    }
     
    // Driver code
    public static void Main ()
    {
         
        int [] arr = { 1, 2, 1, 5, 6 };
        int x = arr.Length;
        int y = 4;
     
        Console.WriteLine(isDivisible(arr, x, y) ? "Yes" : "No");
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
Yes