码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【PAT甲级 - C++题解】1120 Friend Numbers


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1120 Friend Numbers (pintia.cn)
    🔑中文翻译:朋友数
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1120 Friend Numbers

    Two integers are called “friend numbers” if they share the same sum of their digits, and the sum is their “friend ID”. For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID’s among them.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than 104.

    Output Specification:

    For each case, print in the first line the number of different friend ID’s among the given integers. Then in the second line, output the friend ID’s in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.

    Sample Input:

    8
    123 899 51 998 27 33 36 12
    
    • 1
    • 2

    Sample Output:

    4
    3 6 9 26
    
    • 1
    • 2
    题意

    这道题不要混淆了题目条件,题目需要求的是有多少朋友证号,而朋友证号是指每个数的各位之和,比如 123 的朋友证号是 1 + 2 + 3 = 6 ,33 的朋友证号也是 3 + 3 = 6 ,并且 123 和 33 被称为朋友数。但是需要注意的是,并不是说只有被称为朋友数才能被称为朋友证号,所以这道题朋友数这个性质实际上用不到。

    思路

    根据上述题意可以知道我们需要找出有多少个不同的朋友证号,可以利用 set 容器的性质,会帮我们自动过滤掉重复的值,并且为我们自动排序好。所以只需要将每个数的朋友证号计算出来再放进 set 中,最后输出即可。

    代码
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
    
        set<int> nums;
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            //计算各位之和
            int sum = 0;
            while (x) sum += x % 10, x /= 10;
            nums.insert(sum);   //set容器中已经帮我们过滤掉相同的朋友证号了
        }
    
        //输出答案
        cout << nums.size() << endl;
        bool is_first = true;
        for (auto& s : nums)
        {
            if (is_first) is_first = false;
            else cout << " ";
            cout << s;
        }
        cout << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    无管道后门(Backdoor)编写
    《敏捷无敌之DevOps时代》读后感
    leetcode分类刷题:二叉树(二、简单重复逻辑的递归)
    nodejs+vue云旅青城系统-旅游网站
    用无感知的方式为你的数据加上一层缓存
    8┃音视频直播系统之 WebRTC 信令系统实现以及通讯核心并实现视频通话
    shell 编程简记
    【微信小程序】遍历列表数据,循环使用canva生成图片并下载
    Go语言用Colly库编写的图像爬虫程序
    [office] EXCEL表格不能使用键盘箭头切换单元格该怎么解决- #媒体#经验分享#知识分享
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/127823562
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号