• PAT(Advanced Level)A1120. Friend Numbers


    题意

    如果两个数的数位和一样那么这两个数就是友好数,称数位和为友好ID,现在要找出给定的序列中有几个不同的友好ID

    思路

    • 按照要求模拟就好了
    • 记录有几个不同的友好ID,可以采用set

    代码

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <map>
    #include <algorithm>
    #include <string.h>
    #include <set>
    using namespace std;
    int add(int x) {
        int sum = 0;
        while(x) {
            int t = x % 10;
            x /= 10;
            sum += t;
        }
        return sum;
    }
    int main() {
        int n, t;
        cin >> n;
        set<int> s;
        for(int i = 0; i < n; i++) {
            cin >> t;
            int digit_sum = add(t);
            s.insert(digit_sum);
        }
        
        cout << s.size() << endl;
        if(s.size() != 0) {
            for(auto it = s.begin(); it != s.end(); it++) {
                if(it == s.begin())
                    cout << *(it);
                else
                    cout << " " << *(it);
            }
        }
        return 0;
    }
    
    如有转载,请注明出处QAQ
  • 相关阅读:
    js/css 伪类, 获取伪类样式
    JS 盒子模型
    python坑之input获取字符串
    configpraser模块
    类的特殊成员&反射&异常处理
    面向对象
    员工信息表
    re正则表达式
    PyCharm教程
    递归实现jsonTree
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/14530341.html
Copyright © 2020-2023  润新知