zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    3.1C#中的命名空间
    2章总结
    2.4冒泡排序
    2.3 C#中的数组
    2.2二重循环
    2.1c#中的循环语句
    1章总结
    docker内外数据拷贝
    搭建docker环境
    centos7 部署Apache的httpd服务器
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/14530341.html
Copyright © 2011-2022 走看看