C++ 給定基數下的全數字數
包含從 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.
查詢解決方案的方法
為了解決這個問題,我們將使用集合並將每個數字插入集合中,因為我們需要儲存唯一值。
遍歷字串,每次取一個字元。
然後檢查元素是整數還是字母。
如果它是字母,則將其在字母表中的位置加上 10 以表示兩位數。
將值儲存在集合中。
遍歷後,檢查集合的大小是否等於基數。
示例
以上方法的 C++ 程式碼
#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 等程式語言來實現。希望本教程對您有所幫助。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP