PAT B -1064 friend number (20 points)

Click on the link full solution summary PAT B -AC

Title:
If two integer numbers and everybody is the same, is called the "number of friends", and that the public and that their "friend card number." 51 and 123, for example, is the number of friends, because 1 + 2 + 3 = 5 + 1 = 6, and 6 is their friends card number. Given some of the integer requiring you statistics about how many different license number of friends they have.

Input format:
input of the first row is given a positive integer N. Then given N positive integers line, separated by a space between the numbers. Title ensure that all numbers less than 10 . 4 .

Output Format:
First, a first output line given the number of different digital certificate number of friends; friends card number and then outputs a row in ascending order, a digital interval space, and the end of the row may not have extra space.

Sample input:

8
123 899 51 998 27 33 36 12

Sample output:

4
3 6 9 26

My code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

int main()
{
    set<int>friend_num;
    int N;
    cin>>N;
    for(int i=0;i<N;i++)
    {
        int t;
        cin>>t;
        int sum=0;
        while(t>0)
        {
            sum+=t%10;
            t/=10;
        }
        friend_num.insert(sum);
    }
    cout<<friend_num.size()<<endl;;
    while(1)
    {
        printf("%d",*friend_num.begin());
        friend_num.erase(friend_num.begin());
        if(friend_num.size())cout<<" ";
        else return 0;
    }
    return 0;
}

Note: direct calculation and every line, and need not have the same number of two, as in the example 3

Published 82 original articles · won praise 1 · views 1671

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104902866