PAT Level B-Number of Friends

Title description
If the sum of the digits of two integers is the same, it is called the "number of friends", and the public sum is their "friend number".

For example, 123 and 51 are the number of friends, because 1+2+3 = 5+1 = 6, and 6 is their friend ID number.

Given some integers, you are asked to count how many different friend ID numbers are in them.

Input format The
first line of input gives a positive integer N.
The following line gives N positive integers, separated by spaces.

The title guarantees that all numbers are less than 10 ​4 ​​.

Output format
First, the first line outputs the number of different friend ID numbers in a given number; the
next line outputs these friend ID numbers in increasing order, with a space between the numbers, and there must be no extra spaces at the end of the line.

Input sample
8
123 899 51 998 27 33 36 12

Sample output
4
3 6 9 26


Problem solution
STL:

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

int n, x;
set<int> S;

int main()
{
    
    
	cin >> n;
	
	while(n --)
	{
    
    
		cin >> x;
		int sum = 0;
		while(x)
		{
    
    
			sum += x % 10;
			x /= 10;
		}
		S.insert(sum);
	}	
	
	cout << S.size() << endl;
	
	bool flag = true;
	for (auto &x : S)
		if(flag) cout << x, flag = false;
		else cout << ' ' << x; 
		
	return 0;	
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/113850824