Skip to content

Latest commit

 

History

History
57 lines (51 loc) · 1.12 KB

A1120.md

File metadata and controls

57 lines (51 loc) · 1.12 KB

A1120 Friend Numbers

1.题意理解

digit sum一样的两个数字称为friend number。这个和称为他们的friend ID,要求计算不同的个数并升序输出。

2.思路分析

主要考点是位和的计算,可以用对10取余来获得每一位,也可以用字符串。

3.参考代码

#include <bits/stdc++.h>
using namespace std;

int main()
{
    set<int> ans;
    int n;
    scanf("%d", &n);
    string s;
    int value;
    for (int i = 0; i < n; i++)
    {
        value = 0;
        cin >> s;
        for (char c : s)
            value += (c - '0');
        ans.insert(value);
    }
    printf("%d\n", ans.size());
    int cnt = 0;
    for (auto it = ans.begin(); it != ans.end(); it++)
    {
        printf("%d", *it);
        cnt++;
        if (cnt != ans.size())
            printf(" ");
        else
            printf("\n");
    }
    return 0;
}

求位和:

int get_digit_sum(int num)
{
    int sum = 0;

    while(num)
    {
        sum += num % 10; // 取最低位
        num /= 10; // 把最低位去掉
    }

    return sum;
}