包含从0到基数B的所有数字的数字称为该基数下的全数字数。然而,一些数字的数字从1到9,被称为无零全数字数。一些全数字数的例子包括0123456789,0789564312等。
在本教程中,我们将讨论一个问题,我们给定一个数字和一个基数,我们需要检查该数字在给定基数下是否为全数字数,例如−
Input: num = “9651723467380AZ”, base = 10
Output: YES
Explanation: num contains all the digits in the base 10 i.e from 0 to 9, so it is a pandigital number.
Input: num = “130264ABCDE745789”, base = 16
Output: NO
Explanation: num does not contain F(15) which is in the base 16 i.e from 0 to 15, so it is not a pandigital number.
Approach to Find the Solution
To solve this problem, we will use Set and insert each digit in the set because we need to store unique values.
-
Traverse through the string, taking each character at a time.
-
Then check if the element is an integer or alphabet.
-
If it is an alphabet, then add 10 to its position on the alphabet to represent 2-digit.
-
Store the values in the set.
-
After traversing, check whether the size of the set equals to base.
Example
C++ Code for the Above Approach
#include<bits/stdc++.h>
using namespace std;
int main(){
int base = 10;
char n[] = "9651723467380AZ";
// Declaring set to store unique values.
set<int, greater<int> > s;
// Traversing through the string.
for (int i = 0; i < strlen(n); i++){
// Checking if element is Integer.
if (n[i] >= \'0\' && n[i] <= \'9\')
s.insert(n[i]- \'0\');
// Checking if element is alphabet.
else if (n[i] - \'A\' <= base - 11)
s.insert(n[i] - \'A\' + 10) ;
}
// Checking if all the digits are present.
if(s.size()==base)
cout<< "YES";
else
cout<< "NO";
return 0;
}
输出
YES
结论
在本教程中,我们讨论了一个问题,即给定一个数字和基数。我们需要找出该数字是否是全数字的。我们讨论了一种简单的方法来解决这个问题,即通过将值插入到一个集合中,并检查其与基数的大小。我们还讨论了这个问题的C++程序,我们可以使用类似C、Java、Python等编程语言来完成。希望您会发现这个教程有帮助。