首頁  >  文章  >  後端開發  >  在給定的進制下,將C++中的Pandigital數字翻譯成中文

在給定的進制下,將C++中的Pandigital數字翻譯成中文

WBOY
WBOY轉載
2023-08-30 08:01:101163瀏覽

在給定的進制下,將C++中的Pandigital數字翻譯成中文

包含從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] >= &#39;0&#39; && n[i] <= &#39;9&#39;)
           s.insert(n[i]- &#39;0&#39;);
        // Checking if element is alphabet.
        else if (n[i] - &#39;A&#39; <= base - 11)
           s.insert(n[i] - &#39;A&#39; + 10) ;
    }
    // Checking if all the digits are present.
    if(s.size()==base)
       cout<< "YES";
    else
        cout<< "NO";
    return 0;
}

#輸出

YES

結論

在本教程中,我們討論了一個問題,即給定一個數字和基數。我們需要找出該數字是否為全數的。我們討論了一種簡單的方法來解決這個問題,即透過將值插入到一個集合中,並檢查其與基數的大小。我們也討論了這個問題的C 程序,我們可以使用類似C、Java、Python等程式語言來完成。希望您會發現這個教學有幫助。

以上是在給定的進制下,將C++中的Pandigital數字翻譯成中文的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除