📜  检查大数是否可被7整除

📅  最后修改于: 2021-05-04 17:46:34             🧑  作者: Mango

系统会为您提供一个大的n位数字,您必须检查该数字是否可以被7整除。
当且仅当交替的一系列数字(a2 a1 a0)–(a5 a4)时,其数字形式为(ar ar-1 ar-2….a2 a1 a0)的(r + 1)位整数n可被7整除。 a3)+(a8 a7 a6)–…被7整除。

括号内的数字三元组表示数字形式的3位数。

例子 :

Input : 8955795758
Output : Divisible by 7
       Explanation:
       We express the number in terms of triplets 
       of digits as follows.
                (008)(955)(795)(758)
       Now, 758- 795 + 955 - 8 = 910, which is 
       divisible by 7

Input : 100000000000
Output : Not Divisible by 7
       Explanation:
       We express the number in terms of triplets 
       of digits as follows.
                (100)(000)(000)(000)
       Now, 000- 000 + 000 - 100 = -100, which is 
       not divisible by 7

请注意,n中的位数不能为3的倍数。在这种情况下,在取出所有三元组(从n的右侧)以形成最后一个三元组之后,我们在其余数字的左侧涂零。
一种简单有效的方法是采用字符串形式的输入(如果需要,可以通过在数字的左边加0来使其长度为3 * m形式),然后必须将数字从右到左以3为单位添加,直到它变成一个3位数字,形成一个备用序列,并检查该序列是否可被7整除。

此处完成了检查7除数的程序实现。

C++
// C++ code to check divisibility of a
// given large number by 7
#include
using namespace std;
 
int isdivisible7(char num[])
{
    int n = strlen(num), gSum;
    if (n == 0 && num[0] == '\n')
        return 1;
 
    // Append required 0s at the beginning.
    if (n % 3 == 1) {
        strcat(num, "00");
        n += 2;
    }
    else if (n % 3 == 2) {
        strcat(num, "0");
        n++;
    }
 
    // add digits in group of three in gSum
    int i, GSum = 0, p = 1;
    for (i = n - 1; i >= 0; i--) {
 
        // group saves 3-digit group
        int group = 0;
        group += num[i--] - '0';
        group += (num[i--] - '0') * 10;
        group += (num[i] - '0') * 100;
 
        gSum = gSum + group * p;
 
        // generate alternate series of plus
        // and minus
        p *= (-1);
    }
 
    return (gSum % 7 == 0);
}
 
// Driver code
int main()
{
    // Driver method
    char num[] = "8955795758";
    if (isdivisible7(num))
        cout << "Divisible by 7";
    else
        cout << "Not Divisible by 7";
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// C code to check divisibility of a
// given large number by 7
#include 
#include 
int isdivisible7(char num[])
{
    int n = strlen(num), gSum;
    if (n == 0 && num[0] == '\n')
        return 1;
 
    // Append required 0s at the beginning.
    if (n % 3 == 1) {
        strcat(num, "00");
        n += 2;
    }
    else if (n % 3 == 2) {
        strcat(num, "0");
        n++;
    }
 
    // add digits in group of three in gSum
    int i, GSum = 0, p = 1;
    for (i = n - 1; i >= 0; i--) {
 
        // group saves 3-digit group
        int group = 0;
        group += num[i--] - '0';
        group += (num[i--] - '0') * 10;
        group += (num[i] - '0') * 100;
 
        gSum = gSum + group * p;
 
        // generate alternate series of plus
        // and minus
        p *= (-1);
    }
 
    return (gSum % 7 == 0);
}
 
// Driver code
int main()
{
    // Driver method
    char num[] = "8955795758";
    if (isdivisible7(num))
        printf("Divisible by 7");
    else
        printf("Not Divisible by 7");
    return 0;
}


Java
// Java code to check divisibility of a given large number by 7
 
class Test {
    // Method to check divisibility
    static boolean isDivisible7(String num)
    {
        int n = num.length();
        if (n == 0 && num.charAt(0) == '0')
            return true;
 
        // Append required 0s at the beginning.
        if (n % 3 == 1)
            num = "00" + num;
        if (n % 3 == 2)
            num = "0" + num;
        n = num.length();
 
        // add digits in group of three in gSum
        int gSum = 0, p = 1;
        for (int i = n - 1; i >= 0; i--) {
 
            // group saves 3-digit group
            int group = 0;
            group += num.charAt(i--) - '0';
            group += (num.charAt(i--) - '0') * 10;
            group += (num.charAt(i) - '0') * 100;
            gSum = gSum + group * p;
            // generate alternate series of plus and minus
            p = p * -1;
        }
 
        // calculate result till 3 digit sum
        return (gSum % 7 == 0);
    }
 
    // Driver method
    public static void main(String args[])
    {
        String num = "8955795758";
 
        System.out.println(isDivisible7(num) ? "Divisible by 7" : "Not Divisible  by 7");
    }
}


Python3
# Python 3 code to check divisibility
# of a given large number by 7
 
def isdivisible7(num):
    n = len(num)
    if (n == 0 and num[0] == '\n'):
        return 1
 
    # Append required 0s at the beginning.
    if (n % 3 == 1) :
        num = str(num) + "00"
        n += 2
     
    elif (n % 3 == 2) :
        num = str(num) + "0"
        n += 1
 
    # add digits in group of three in gSum
    GSum = 0
    p = 1
    for i in range(n - 1, -1, -1) :
 
        # group saves 3-digit group
        group = 0
        group += ord(num[i]) - ord('0')
        i -= 1
        group += (ord(num[i]) - ord('0')) * 10
        i -= 1
        group += (ord(num[i]) - ord('0')) * 100
 
        GSum = GSum + group * p
 
        # generate alternate series of
        # plus and minus
        p *= (-1)
 
    return (GSum % 7 == 0)
 
# Driver code
if __name__ == "__main__":
     
    num = "8955795758"
    if (isdivisible7(num)):
        print("Divisible by 7")
    else :
        print("Not Divisible by 7")
 
# This code is contributed by ChitraNayal


C#
// C# code to check divisibility of a
// given large number by 7
using System;
 
class GFG {
 
    // Method to check divisibility
    static bool isDivisible7(String num)
    {
        int n = num.Length;
        if (n == 0 && num[0] == '0')
            return true;
 
        // Append required 0s at the beginning.
        if (n % 3 == 1)
            num = "00" + num;
 
        if (n % 3 == 2)
            num = "0" + num;
 
        n = num.Length;
 
        // add digits in group of three in gSum
        int gSum = 0, p = 1;
        for (int i = n - 1; i >= 0; i--) {
 
            // group saves 3-digit group
            int group = 0;
            group += num[i--] - '0';
            group += (num[i--] - '0') * 10;
            group += (num[i] - '0') * 100;
            gSum = gSum + group * p;
 
            // generate alternate series
            // of plus and minus
            p = p * -1;
        }
 
        // calculate result till 3 digit sum
        return (gSum % 7 == 0);
    }
 
    // Driver code
    static public void Main()
    {
        String num = "8955795758";
 
        // Function calling
        Console.WriteLine(isDivisible7(num) ? "Divisible by 7" : "Not Divisible by 7");
    }
}
 
// This code is contributed by Ajit.


PHP
= 0; $i--)
    {
 
        // group saves 3-digit group
        $group = 0;
        $group += $num[$i--] - '0';
        $group += ($num[$i--] - '0') * 10;
        $group += ($num[$i] - '0') * 100;
        $gSum = $gSum + $group * $p;
         
        // generate alternate series
        // of plus and minus
        $p = $p * -1;
    }
 
    // calculate result till 3 digit sum
    return ($gSum % 7 == 0);
}
 
// Driver Code
$num = "8955795758";
 
echo (isDivisible7($num) ?
        "Divisible by 7" :
        "Not Divisible by 7");
 
// This code is contributed by Ryuga
?>


Javascript


输出:

Divisible by 7