您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页1120 Friend Numbers【set 去重 超简单思路】

1120 Friend Numbers【set 去重 超简单思路】

来源:五一七教育网

Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID's among them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than 104.

Output Specification:

For each case, print in the first line the number of different friend ID's among the given integers. Then in the second line, output the friend ID's in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.

Sample Input:

8
123 9 51 998 27 33 36 12

Sample Output:

4
3 6 9 26

思路:输入n个字符串,对字符串的每一位转成数字求和,最后存入到set种去重(默认按照从小到大)最后按照题要求格式输出(最后一个数字后面不能有空格)

#include<bits/stdc++.h>
using namespace std;
set<int>st;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    //cin.ignore();
    string s;
    while(n--){
         cin>>s;
        int len=s.size();
        int sum=0;
        for(int i=0;i<len;i++){
          sum+=(s[i]-'0');
        }
        st.insert(sum);
    }
    cout<<st.size()<<'\n';
    for(auto it=st.begin();it!=st.end();it++){
       if(it!=st.begin()) cout<<" ";
       cout<<*it;
    }
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务