📌  相关文章
📜  可以被K整除并由仅用1组成的最小数字的长度

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

给定整数K ,任务是找到最小的no的长度。 N可被K整除并且仅使用1作为其数字形成的N。如果不存在这样的数字,则打印-1
例子:

天真的方法:

  1. 首先,我们必须检查K2还是5的倍数,然后答案将是-1,因为没有任何数字可以通过仅将1用作其数字被25整除。
  2. 现在遍历所有可能的否。最多使用1个K来形成1 ,然后检查其与K的可除性。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return length
// of the resulatant number
int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++) {
 
        // Generate all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        number = number * 10 + 1;
 
        // If number is divisible by k
        // then return the length
        if ((number % K == 0))
            return len;
    }
 
    return -1;
}
 
// Driver code
int main()
{
 
    int K = 7;
    cout << numLen(K);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function to return length
    // of the resulatant number
    static int numLen(int K)
    {
 
        // If K is a multiple of 2 or 5
        if (K % 2 == 0 || K % 5 == 0)
        {
            return -1;
        }
 
        int number = 0;
 
        int len = 1;
 
        for (len = 1; len <= K; len++)
        {
 
            // Generate all possible numbers
            // 1, 11, 111, 111, ..., K 1's
            number = number * 10 + 1;
 
            // If number is divisible by k
            // then return the length
            if ((number % K == 0))
            {
                return len;
            }
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int K = 7;
        System.out.println(numLen(K));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python implementation of the approach
 
# Function to return length
# of the resulatant number
def numLen(K):
 
    # If K is a multiple of 2 or 5
    if (K % 2 == 0 or K % 5 == 0):
        return -1;
 
    number = 0;
 
    len = 1;
 
    for len in range(1,K+1):
 
        # Generate all possible numbers
        # 1, 11, 111, 111, ..., K 1's
        number = number * 10 + 1;
 
        # If number is divisible by k
        # then return the length
        if ((number % K == 0)):
            return len;
 
    return -1;
 
# Driver code
K = 7;
print(numLen(K));
 
# This code contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return length
// of the resulatant number
static int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++)
    {
 
        // Generate all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        number = number * 10 + 1;
 
        // If number is divisible by k
        // then return the length
        if ((number % K == 0))
            return len;
    }
 
    return -1;
}
 
// Driver code
static void Main()
{
    int K = 7;
    Console.WriteLine(numLen(K));
}
}
 
// This code is contributed by mits


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return length
// of the resulatant number
int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++) {
 
        // Instead of generating all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        // Take remainder with K
        number = (number * 10 + 1) % K;
 
        // If number is divisible by k
        // then remainder will be 0
        if (number == 0)
            return len;
    }
 
    return -1;
}
 
// Driver code
int main()
{
 
    int K = 7;
    cout << numLen(K);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return length
    // of the resulatant number
    public static int numLen(int K)
    {
 
        // If K is a multiple of 2 or 5
        if (K % 2 == 0 || K % 5 == 0)
            return -1;
 
        int number = 0;
 
        int len = 1;
 
        for (len = 1; len <= K; len++) {
 
            // Instead of generating all possible numbers
            // 1, 11, 111, 111, ..., K 1's
            // Take remainder with K
            number = (number * 10 + 1) % K;
 
            // If number is divisible by k
            // then remainder will be 0
            if (number == 0)
                return len;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int K = 7;
        System.out.print(numLen(K));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return length
# of the resulatant number
def numLen(K):
     
    # If K is a multiple of 2 or 5
    if(K % 2 == 0 or K % 5 == 0):
        return -1
 
    number = 0
 
    len = 1
 
    for len in range (1, K + 1):
         
        # Instead of generating all possible numbers
        # 1, 11, 111, 111, ..., K 1's
        # Take remainder with K
        number = ( number * 10 + 1 ) % K
     
        # If number is divisible by k
        # then remainder will be 0
        if number == 0:
            return len
 
    return -1
 
# Driver code
K = 7
print(numLen(K))


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return length
// of the resulatant number
public static int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++)
    {
 
        // Instead of generating all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        // Take remainder with K
        number = (number * 10 + 1) % K;
 
        // If number is divisible by k
        // then remainder will be 0
        if (number == 0)
            return len;
    }
 
    return -1;
}
 
// Driver code
public static void Main()
{
    int K = 7;
    Console.WriteLine(numLen(K));
}
}
 
// This code is contirbuted by Ryuga


PHP


Javascript


输出:
6

高效的方法:正如我们在上述方法中看到的那样,我们生成所有可能的数字,例如1、11、1111、11111,…,K次,但如果K的值非常大,则否。将超出数据类型的范围,因此我们可以利用模块化属性。
代替数字=数字* 10 +1 ,我们可以做得更好,因为数字=(数字* 10 +1)%K
说明:我们从number = 1开始,重复做number = number * 10 + 1,然后在每次迭代中,我们将得到上述序列的新项。

由于我们在每一步都重复取数字的余数,因此每一步都有newNum = oldNum * 10 +1 。通过模算术(a * b + c)%m的规则与(((a * b)%m + c%m)%m 。因此,无论oldNum是余数还是原始数字都无所谓,答案是正确的。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return length
// of the resulatant number
int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++) {
 
        // Instead of generating all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        // Take remainder with K
        number = (number * 10 + 1) % K;
 
        // If number is divisible by k
        // then remainder will be 0
        if (number == 0)
            return len;
    }
 
    return -1;
}
 
// Driver code
int main()
{
 
    int K = 7;
    cout << numLen(K);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG {
 
    // Function to return length
    // of the resulatant number
    public static int numLen(int K)
    {
 
        // If K is a multiple of 2 or 5
        if (K % 2 == 0 || K % 5 == 0)
            return -1;
 
        int number = 0;
 
        int len = 1;
 
        for (len = 1; len <= K; len++) {
 
            // Instead of generating all possible numbers
            // 1, 11, 111, 111, ..., K 1's
            // Take remainder with K
            number = (number * 10 + 1) % K;
 
            // If number is divisible by k
            // then remainder will be 0
            if (number == 0)
                return len;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int K = 7;
        System.out.print(numLen(K));
    }
}

Python3

# Python3 implementation of the approach
 
# Function to return length
# of the resulatant number
def numLen(K):
     
    # If K is a multiple of 2 or 5
    if(K % 2 == 0 or K % 5 == 0):
        return -1
 
    number = 0
 
    len = 1
 
    for len in range (1, K + 1):
         
        # Instead of generating all possible numbers
        # 1, 11, 111, 111, ..., K 1's
        # Take remainder with K
        number = ( number * 10 + 1 ) % K
     
        # If number is divisible by k
        # then remainder will be 0
        if number == 0:
            return len
 
    return -1
 
# Driver code
K = 7
print(numLen(K))

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return length
// of the resulatant number
public static int numLen(int K)
{
 
    // If K is a multiple of 2 or 5
    if (K % 2 == 0 || K % 5 == 0)
        return -1;
 
    int number = 0;
 
    int len = 1;
 
    for (len = 1; len <= K; len++)
    {
 
        // Instead of generating all possible numbers
        // 1, 11, 111, 111, ..., K 1's
        // Take remainder with K
        number = (number * 10 + 1) % K;
 
        // If number is divisible by k
        // then remainder will be 0
        if (number == 0)
            return len;
    }
 
    return -1;
}
 
// Driver code
public static void Main()
{
    int K = 7;
    Console.WriteLine(numLen(K));
}
}
 
// This code is contirbuted by Ryuga

的PHP


Java脚本


输出:
6

时间复杂度: O(K)
辅助空间: O(1)