📜  检查大数是否可以被11整除

📅  最后修改于: 2021-05-06 22:17:17             🧑  作者: Mango

给定一个数字,任务是检查该数字是否可被11整除。输入数字可能很大,即使我们使用long long int,也可能无法存储它。
例子:

Input : n = 76945
Output : Yes

Input  : n = 1234567589333892
Output : Yes

Input  : n = 363588395960667043875487
Output : No

由于输入数字可能很大,因此我们不能使用n%11来检查数字是否可以被11整除,尤其是在C / C++之类的语言中。这个想法是基于以下事实。
如果后两个数之差可被11整除,则数字可被11整除。

  1. 奇数位的数字总和。
  2. 偶数位的数字总和。

插图:

For example, let us consider 76945 
Sum of digits at odd places  : 7 + 9 + 5
Sum of digits at even places : 6 + 4 
Difference of two sums = 21 - 10 = 11
Since difference is divisible by 11, the
number 7945 is divisible by 11.

这是如何运作的?

Let us consider 7694, we can write it as
7694 = 7*1000 + 6*100 + 9*10 + 4

The proof is based on below observation:
Remainder of 10i divided by 11 is 1 if i is even
Remainder of 10i divided by 11 is -1 if i is odd

So the powers of 10 only result in values either 1 
or -1. 

Remainder of "7*1000 + 6*100 + 9*10 + 4"
divided by 11 can be written as : 
7*(-1) + 6*1 + 9*(-1) + 4*1

The above expression is basically difference 
between sum of even digits and odd digits.

以下是上述事实的实现:

C++
// C++ program to find if a number is divisible by
// 11 or not
#include
using namespace std;
 
// Function to find that number divisible by 11 or not
int check(string str)
{
    int n = str.length();
 
    // Compute sum of even and odd digit
    // sums
    int oddDigSum = 0, evenDigSum = 0;
    for (int i=0; i


Java
// Java program to find if a number is
// divisible by 11 or not
class IsDivisible
{
    // Function to find that number divisible by 11 or not
    static boolean check(String str)
    {
        int n = str.length();
      
        // Compute sum of even and odd digit
        // sums
        int oddDigSum = 0, evenDigSum = 0;
        for (int i=0; i


Python3
# Python 3 code program to find if a number
# is divisible by 11 or not
 
 
# Function to find that number divisible by
#  11 or not
def check(st) :
    n = len(st)
 
    # Compute sum of even and odd digit
    # sums
    oddDigSum = 0
    evenDigSum = 0
    for i in range(0,n) :
        # When i is even, position of digit is odd
        if (i % 2 == 0) :
            oddDigSum = oddDigSum + ((int)(st[i]))
        else:
            evenDigSum = evenDigSum + ((int)(st[i]))
     
     
    # Check its difference is divisible by 11 or not
    return ((oddDigSum - evenDigSum) % 11 == 0)
 
# Driver code
st = "76945"
if(check(st)) :
    print( "Yes")
else :
    print("No ")
     
# This code is contributed by Nikita tiwari.


C#
// C# program to find if a number is
// divisible by 11 or not
using System;
 
class GFG
{
    // Function to find that number
    // divisible by 11 or not
    static bool check(string str)
    {
        int n = str.Length;
     
        // Compute sum of even and odd digit
        // sums
        int oddDigSum = 0, evenDigSum = 0;
         
        for (int i = 0; i < n; i++)
        {
            // When i is even, position of
            // digit is odd
            if (i % 2 == 0)
                oddDigSum += (str[i] - '0');
            else
                evenDigSum += (str[i] - '0');
        }
     
        // Check its difference is
        // divisible by 11 or not
        return ((oddDigSum - evenDigSum)
                                % 11 == 0);
    }
     
    // main function
    public static void Main ()
    {
        String str = "76945";
         
        if(check(str))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

Yes