给定整数K ,任务是找到最小的no的长度。 N可被K整除并且仅使用1作为其数字形成的N。如果不存在这样的数字,则打印-1
例子:
Input: K = 3
Output: 3
111 is the smallest number formed by using 1 only
which is divisible by 3.
Input: K = 7
Output: 6
111111 is the required number.
Input: K = 12
Output: -1
天真的方法:
- 首先,我们必须检查K是2还是5的倍数,然后答案将是-1,因为没有任何数字可以通过仅将1用作其数字被2或5整除。
- 现在遍历所有可能的否。最多使用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,然后在每次迭代中,我们将得到上述序列的新项。
1*10 + 1 = 11
11*10 + 1 = 111
111*10 + 1 = 1111
1111*10 + 1 = 11111
11111*10 + 1 = 111111
由于我们在每一步都重复取数字的余数,因此每一步都有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)