📌  相关文章
📜  检查给定数字是否可折

📅  最后修改于: 2021-05-06 20:51:18             🧑  作者: Mango

给定一个整数n,找出n是否为可整除的。在数学中,如果数字遵循某些独特的性质,则该数字称为“可折除的数”。该数字不应包含任何前导零。输入数字的前i个数字组成的数字应可被i整除,其中i > 1 ~and ~i <= number ~of ~digits ~in ~the ~input ~number 。如果遵循这些属性的任何数字,则称为“可折数”
例子:

Input: 345654
Output: 345654 is Polydivisible number.
Explanation: 
The first digit of the number is non-zero. 
The number formed by the first 2 digits(34) 
is divisible by 2. The number formed by the
first 3 digits(345) is divisible by 3. 
The number formed by the first 4 digits(3456)
is divisible by 4. The number formed by the
first 5 digits(34565) is divisible by 5. 
The number formed by the first 6 digits(345654)
is divisible by 6.     

Input: 130
Output: 130 is Not Polydivisible number.

Input: 129
Output: 129 is Polydivisible number.    

方法:这个想法很简单。

下面是上述方法的实现。

C++
// CPP program to check whether
// a number is polydivisible or not
#include 
using namespace std;
  
// function to check polydivisible
// number
void check_polydivisible(int n)
{
    int N = n;
    vector digit;
  
    // digit extraction of input number
    while (n > 0) {
  
        // store the digits in an array
        digit.push_back(n % 10);
        n /= 10;
    }
    reverse(digit.begin(), digit.end());
  
    bool flag = true;
    n = digit[0];
    for (int i = 1; i < digit.size(); i++) {
  
        // n contains first i digits
        n = n * 10 + digit[i];
  
        // n should be divisible by i
        if (n % (i + 1) != 0) {
            flag = false;
            break;
        }
    }
    if (flag)
        cout << N << " is Polydivisible number.";
    else
        cout << N << " is Not Polydivisible number.";
}
  
int main()
{
    int n = 345654;
    check_polydivisible(n);
}


Java
// Java program to check whether
// a number is polydivisible or not
import java.util.*;
import java.io.*;
  
class GFG {
      
    // function to check polydivisible
    // number
    static void check_polydivisible(int n)
    {
        int N = n;
        Vector digit = new Vector();
      
        // digit extraction of input number
        while (n > 0) {
      
            // store the digits in an array
            digit.add(new Integer(n % 10));
            n /= 10;
        }
        Collections.reverse(digit);
      
        boolean flag = true;
        n = digit.get(0);
        for (int i = 1; i < digit.size(); i++) {
      
            // n contains first i digits
            n = n * 10 + digit.get(i);
      
            // n should be divisible by i
            if (n % (i + 1) != 0) {
                flag = false;
                break;
            }
        }
        if (flag)
            System.out.println(N + " is Polydivisible number.");
        else
            System.out.println(N + " is Not Polydivisible number.");
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int n = 345654;
        check_polydivisible(n);
    }
}


Python3
# Python 3 program to check whether
# a number is polydivisible or not
  
# function to check polydivisible
# number
def check_polydivisible(n):
    N = n
    digit = []
  
    # digit extraction of input number
    while (n > 0):
          
        # store the digits in an array
        digit.append(n % 10)
        n /= 10
  
    digit.reverse()
  
    flag = True
    n = digit[0]
    for i in range(1, len(digit), 1):
          
        # n contains first i digits
        n = n * 10 + digit[i]
  
        # n should be divisible by i
        if (n % (i + 1) != 0):
            flag = False
            break
      
    if (flag):
        print(N, "is Polydivisible number.");
    else:
        print(N, "is Not Polydivisible number.")
  
# Driver Code
if __name__ == '__main__':
    n = 345654
    check_polydivisible(n)
  
      
# This code is contributed by
# Sahil_Shelangia


C#
// C# program to check whether
// a number is polydivisible or not
using System;
using System.Collections.Generic;
  
class GFG 
{
      
    // function to check polydivisible
    // number
    static void check_polydivisible(int n)
    {
        int N = n;
        List digit = new List();
      
        // digit extraction of input number
        while (n > 0) 
        {
      
            // store the digits in an array
            digit.Add((int)n % 10);
            n /= 10;
        }
        digit.Reverse();
      
        bool flag = true;
        n = digit[0];
        for (int i = 1; i < digit.Count; i++) 
        {
      
            // n contains first i digits
            n = n * 10 + digit[i];
      
            // n should be divisible by i
            if (n % (i + 1) != 0)
            {
                flag = false;
                break;
            }
        }
        if (flag)
            Console.WriteLine(N + 
                    " is Polydivisible number.");
        else
            Console.WriteLine(N + 
                    " is Not Polydivisible number.");
    }
      
    // Driver code
    public static void Main (String[] args) 
    {
        int n = 345654;
        check_polydivisible(n);
    }
}
  
// This code is contributed by Rajput-Ji


输出:
345654 is Polydivisible number.