当前位置: 代码迷 >> 综合 >> 个人练习-PAT甲级-1120 Friend Numbers
  详细解决方案

个人练习-PAT甲级-1120 Friend Numbers

热度:0   发布时间:2023-12-21 11:07:08.0

题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805352925609984

题目大意:给出一个十进制数,其所有位的和是它的ID,ID相同的数叫朋友数。给出N个数字,求有几个不同的ID,并将这些ID排序输出。

思路:简单题,正常算即可。

完整代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>using namespace std;bool known[37];
vector<int> arr;int calId(int num) {
    int sum = 0;while (num != 0) {
    sum += num % 10;num /= 10;}return sum;
}int main() {
    int N;scanf("%d", &N);for (int i = 0; i < N; i++) {
    int num, id;scanf("%d", &num);id = calId(num);if (!known[id]) {
    known[id] = true;arr.push_back(id);}}sort(arr.begin(), arr.end());printf("%d\n%d", arr.size(), arr[0]);for (int i = 1; i < arr.size(); i++)printf(" %d", arr[i]);return 0;
}