首页 > 后端开发 > C++ > 正文

在给定的进制下,将C++中的Pandigital数字翻译成中文

WBOY
发布: 2023-08-30 08:01:10
转载
1277人浏览过

在给定的进制下,将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.

    立即学习C++免费学习笔记(深入)”;

  • Then check if the element is an integer or alphabet.

    火山翻译
    火山翻译

    火山翻译,字节跳动旗下的机器翻译品牌,支持超过100种语种的免费在线翻译,并支持多种领域翻译

    火山翻译 193
    查看详情 火山翻译
  • 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等编程语言来完成。希望您会发现这个教程有帮助。

以上就是在给定的进制下,将C++中的Pandigital数字翻译成中文的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号