给定整数n及其底数b 。任务是检查给定号码是否为给定基数中的Pandigital号码。 Pandigital数字是一个整数,该整数的基数至少有一次。
可以假定基数小于或等于36。在基数36中,数字为[0,1,…9。 A,B,… Z]
例子 :
Input : n = "9651723480", b = 10
Output : Yes
Given number n has all digits from 0 to 9
Input : n = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
b = 36
Output : No
Given number n doesn't have all digits in base
36. For example 1 is missing.
制作一个布尔散列数组,其大小等于该数字的基数,并使用false对其进行初始化。现在,将数字的每个数字迭代,将其对应的索引值在哈希数组中标记为true。最后,检查是否标记了哈希数组中的所有值,如果标记为“ Yes”,即Pandigital number,否则为“ No”。
以下是此方法的实现:
C++
// C++ program to check if a number is pandigital
// in given base.
#include
using namespace std;
// Return true if n is pandigit else return false.
bool checkPandigital(int b, char n[])
{
// Checking length is less than base
if (strlen(n) < b)
return false;
bool hash[b];
memset(hash, false, sizeof(hash));
// Traversing each digit of the number.
for (int i = 0; i < strlen(n); i++)
{
// If digit is integer
if (n[i] >= '0' && n[i] <= '9')
hash[n[i] - '0'] = true;
// If digit is alphabet
else if (n[i] - 'A' <= b - 11)
hash[n[i] - 'A' + 10] = true;
}
// Checking hash array, if any index is
// unmarked.
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
// Driven Program
int main()
{
int b = 13;
char n[] = "1298450376ABC";
(checkPandigital(b, n))? (cout << "Yes" << endl):
(cout << "No" << endl);
return 0;
}
Java
// Java program to check if a number
// is pandigital in given base.
import java.util.*;
class GFG {
// Return true if n is pandigit
// else return false.
static boolean checkPandigital(int b, String n) {
// Checking length is less than base
if (n.length() < b)
return false;
boolean hash[] = new boolean[b];
Arrays.fill(hash, false);
// Traversing each digit of the number.
for (int i = 0; i < n.length(); i++) {
// If digit is integer
if (n.charAt(i) >= '0' && n.charAt(i) <= '9')
hash[n.charAt(i) - '0'] = true;
// If digit is alphabet
else if (n.charAt(i) - 'A' <= b - 11)
hash[n.charAt(i) - 'A' + 10] = true;
}
// Checking hash array, if any
// index is unmarked.
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
int b = 13;
String n = "1298450376ABC";
if (checkPandigital(b, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to check if a number is
# pandigital in given base.
# Return true if n is pandigit else return false.
def checkPandigital(b, n):
# Checking length is less than base
if (len(n) < b):
return 0;
hash = [0] * b;
# Traversing each digit of the number.
for i in range(len(n)):
# If digit is integer
if (n[i] >= '0' and n[i] <= '9'):
hash[ord(n[i]) - ord('0')] = 1;
# If digit is alphabet
elif (ord(n[i]) - ord('A') <= b - 11):
hash[ord(n[i]) - ord('A') + 10] = 1;
# Checking hash array, if any index is
# unmarked.
for i in range(b):
if (hash[i] == 0):
return 0;
return 1;
# Driver Code
b = 13;
n = "1298450376ABC";
if(checkPandigital(b, n)):
print("Yes");
else:
print("No");
# This code is contributed by mits
C#
// C# program to check if a number
// is pandigital in given base.
using System;
class GFG {
// Return true if n is pandigit
// else return false.
static bool checkPandigital(int b, string n) {
// Checking length is less than base
if (n.Length < b)
return false;
bool []hash = new bool[b];
for(int i = 0; i < b; i++)
hash[i] = false;
// Traversing each digit of the number.
for (int i = 0; i < n.Length; i++) {
// If digit is integer
if (n[i] >= '0' && n[i] <= '9')
hash[n[i] - '0'] = true;
// If digit is alphabet
else if (n[i] - 'A' <= b - 11)
hash[n[i] - 'A' + 10] = true;
}
// Checking hash array, if any
// index is unmarked.
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
// Driver code
public static void Main()
{
int b = 13;
String n = "1298450376ABC";
if (checkPandigital(b, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by nitin mittal.
PHP
= '0' && $n[$i] <= '9')
$hash[$n[$i] - '0'] = 1;
// If digit is alphabet
else if (ord($n[$i]) - ord('A') <= $b - 11)
$hash[ord($n[$i]) - ord('A') + 10] = 1;
}
// Checking hash array, if any index is
// unmarked.
for ($i = 0; $i < $b; $i++)
if ($hash[$i] == 0)
return 0;
return 1;
}
// Driven Program
$b = 13;
$n = "1298450376ABC";
if(checkPandigital($b, $n))
echo "Yes";
else
echo "No";
// This code is contributed by Sam007.
?>
输出:
Yes
参考:
https://zh.wikipedia.org/wiki/Pandigital_number