Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-08-30 08:01:10
forward
1211 people have browsed it

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.
Copy after login

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;
}
Copy after login

Output

YES
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template