其他分享
首页 > 其他分享> > PAT(Advanced Level)A1120. Friend Numbers

PAT(Advanced Level)A1120. Friend Numbers

作者:互联网

题意

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

思路

代码

#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;
}

标签:A1120,set,PAT,Level,int,sum,include,ID,友好
来源: https://www.cnblogs.com/MartinLwx/p/14530341.html