📜  查找一个N位数,使其不能被其任何位数整除

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

给定整数N ,任务是找到任何N位正数(零除外),以使它不能被其任何数位整除。如果找不到任何这样的数字,则打印-1

注意:同一N位数字可以有多个这样的数字。

例子:

Input: N = 2  
Output: 23
23 is not divisible by 2 or 3

Input: N = 3
Output: 239

方法:
可以借助数字“ 4”和“ 5”来考虑最简单的解决方案。

  1. 因为要使数字可被5整除,所以该数字必须以0或5结尾;为了使它能被4整除,如果数字必须能被4整除,则最后两位数字。
  2. 因此,可以应用快捷方法来防止4和5的除数准则,它们是:
    • 为防止数字被5整除,该数字除最后一位外,每隔一个数字可包含5。
      Therefore for N digit number,
      (N - 1) digits must be 5 = 5555...(N-1 times)d
      where d is the Nth digit
      
    • 为防止数字被4整除,该数字在最后两位数字可以包含5,在最后一位数字可以包含4。
      Therefore for N digit number,
      Last digit must be 4 = 5555...(N-1 times)4
      

下面是上述方法的实现:

CPP
// CPP program to find N digit number such
// that it is not divisible by any of its digits
  
#include 
using namespace std;
  
// Function that print the answer
void findTheNumber(int n)
{
    // if n == 1 then it is
    // not possible
    if (n == 1) {
        cout << "Impossible" << endl;
        return;
    }
  
    // loop to n-1 times
    for (int i = 0; i < n - 1; i++) {
        cout << "5";
    }
  
    // print 4 as last digit of
    // the number
    cout << "4";
}
  
// Driver code
int main()
{
    int n = 12;
  
    // Function call
    findTheNumber(n);
  
    return 0;
}


Java
// JAVA program to find N digit number such
// that it is not divisible by any of its digits
class GFG{
   
// Function that print the answer
static void findTheNumber(int n)
{
    // if n == 1 then it is
    // not possible
    if (n == 1) {
        System.out.print("Impossible" +"\n");
        return;
    }
   
    // loop to n-1 times
    for (int i = 0; i < n - 1; i++) {
        System.out.print("5");
    }
   
    // print 4 as last digit of
    // the number
    System.out.print("4");
}
   
// Driver code
public static void main(String[] args)
{
    int n = 12;
   
    // Function call
    findTheNumber(n);
   
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find N digit number such
# that it is not divisible by any of its digits
   
# Function that prthe answer
def findTheNumber(n):
    # if n == 1 then it is
    # not possible
    if (n == 1):
        print("Impossible")
        return
   
    # loop to n-1 times
    for i in range(n-1):
        print("5",end="")
   
    # print as last digit of
    # the number
    print("4")
   
# Driver code
if __name__ == '__main__':
    n = 12
   
    #Function call
    findTheNumber(n)
  
# This code is contributed by mohit kumar 29


C#
// C# program to find N digit number such
// that it is not divisible by any of its digits
using System;
  
class GFG{
  
// Function that print the answer
static void findTheNumber(int n)
{
    // if n == 1 then it is
    // not possible
    if (n == 1) {
        Console.Write("Impossible" +"\n");
        return;
    }
  
    // loop to n-1 times
    for (int i = 0; i < n - 1; i++) {
        Console.Write("5");
    }
  
    // print 4 as last digit of
    // the number
    Console.Write("4");
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 12;
  
    // Function call
    findTheNumber(n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
555555555554

时间复杂度: 0(N)