给定一个整数N ,任务是检查N是一个超级d号。
Super-D Number is a number N such that D*ND contains a substring made of D digits containing D only, where D is more than 1 and less than 10
例子:
Input: N = 261
Output: Yes
Explanation:
It will be true for D = 3
D*ND = 3*2613 = 53338743
which contains a substring made of 3 digits 333 containing 3 only.
Input: N = 10
Output: No
方法:这个想法是创建每个可能的字符串,将数字D,D串联很多次,然后检查串联是否以D * N D中的子字符串形式出现,其中D的范围为[2,9] 。
下面是上述方法的实现:
Java
// Java implementation to
// check if N is a super-d number
class GFG{
// Function to check if N
// is a super-d number
static boolean isSuperdNum(int n)
{
for (int d = 2; d < 10; d++)
{
String subString = newString(d);
if (String.valueOf(
(d * Math.pow(n, d))).contains(subString))
return true;
}
return false;
}
// Driver Code
private static String newString(int d)
{
String ans = "";
for (int i = 0; i < d; i++)
{
ans += String.valueOf(d);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 261;
if (isSuperdNum(n) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to
# check if N is a super-d number
# Function to check if N
# is a super-d number
def isSuperdNum(n):
for d in range (2, 10):
substring = str(d) * d;
if substring in str(d * pow(n, d)):
return True
return False
# Driver Code
n = 261
if isSuperdNum(n) == True:
print("Yes")
else :
print("No")
C#
// C# implementation to
// check if N is a super-d number
using System;
class GFG{
// Function to check if N
// is a super-d number
static bool isSuperdNum(int n)
{
for(int d = 2; d < 10; d++)
{
String subString = newString(d);
if (String.Join("",
(d * Math.Pow(n, d))).Contains(subString))
return true;
}
return false;
}
private static String newString(int d)
{
String ans = "";
for(int i = 0; i < d; i++)
{
ans += String.Join("", d);
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int n = 261;
if (isSuperdNum(n) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Rajput-Ji
输出:
Yes
参考文献: OEIS