Home  >  Article  >  Backend Development  >  Translate Pandigital numbers in C++ into Chinese under the given base system

Translate Pandigital numbers in C++ into Chinese under the given base system

WBOY
WBOYforward
2023-08-30 08:01:101163browse

Translate Pandigital numbers in C++ into Chinese under the given base system

A number that contains all digits from 0 to base B is called a total number in that base. However, some numbers have digits from 1 to 9 and are called zero-free full digit numbers. Some examples of fully numeric numbers include 0123456789, 0789564312, etc.

In this tutorial, we will discuss a problem where we are given a number and a base and we need to check if the number is a fully numeric number in the given base, for example -

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;
}

Output

YES

Conclusion

In this tutorial, we discussed a problem given a number and a base. We need to find out if the number is fully numeric. We discussed a simple way to solve this problem by inserting the value into a set and checking its size against the cardinality. We also discussed C programming for this problem, which we can do using programming languages ​​like C, Java, Python, etc. Hope you find this tutorial helpful.

The above is the detailed content of Translate Pandigital numbers in C++ into Chinese under the given base system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete