当前位置:网站首页>PAT B1064

PAT B1064

2022-06-25 19:57:00 Madness makes freedom

1064 Number of friends (20 branch )

If the sum of two integers is the same , It's called “ Number of friends ”, And the public one is theirs “ Friend ID number ”. for example 123 and 51 It's the number of friends , because 1+2+3 = 5+1 = 6, and 6 It's their friend ID number . Given some integers , Ask you to count how many different ID numbers they have .

Input format :

Enter the first line to give a positive integer N. The next line shows N A positive integer , Numbers are separated by spaces . Make sure that all numbers are less than 104.

Output format :

First, the first line outputs the number of different friend ID numbers in the given number ; The next line outputs these ID numbers in increasing order , The numbers are separated by a 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

sample output :

4
3 6 9 26

This question directly uses one set The container stores the friend ID number , It's a lot easier .

#include <iostream>
#include <set>
using namespace std;

int main()
{
    int n,num;
    set<int> friend_id;
    cin >> n;
    while(n--)
    {
        cin >> num;
        int sum=0;
        while(num!=0)
        {
            int a=num%10;
            num/=10;
            sum+=a;
        }
        friend_id.insert(sum);
    }
    int len=friend_id.size();
    cout << len << endl;
    for(auto it=friend_id.begin();it!=friend_id.end();++it)
    {
        --len;
        cout << *it;
        if(len>0)
            cout << ' ';
        else
            cout << endl;
    }
    return 0;
}

原网站

版权声明
本文为[Madness makes freedom]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190512260211.html