PAT (Advanced Level) Practice A1120 Friend Numbers (20 分)(C++)(甲级)(set)

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/88431243

原题链接

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

const int MAX = 10010, INF = 1<<30;
int N, K, Q;
set<int> S;

int main()
{
    int N;
    char str[10];
    scanf("%d", &N);
    for(int i=0; i<N; i++)
    {
        scanf("%s", str);
        int len = strlen(str), sum = 0;
        for(int j=0; j<len; j++)
        {
            sum += str[j] - '0';
        }
        S.insert(sum);
    }
    printf("%d\n", S.size());
    for(set<int>::iterator it = S.begin(); it != S.end(); it++)
    {
        if(it != S.begin()) printf(" ");
        printf("%d", *it);
    }
	return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/88431243