PAT 1120 Friend Numbers[简单]

1120 Friend Numbers (20 分)

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 frind 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 10?4??.

Output Specification:

For each case, print in the first line the number of different frind 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 899 51 998 27 33 36 12

Sample Output:

4
3 6 9 26

题目大意:两个数如果每位相加结果相等,那么结果就是个友好数,现在给出几个数,求他们的友好数。

//就算一个数没有和别的单位数和一样的,也将这个算作友好数。

//我的AC

#include <iostream>
#include <map>
using namespace std;

int main() {
    int n;
    map<int,int> mp;
    cin>>n;
    int t;
    for(int i=0;i<n;i++){
        cin>>t;
        int x,sum=0;
        while(t!=0){
            x=t%10;
            t/=10;
            sum+=x;
        }
        mp[sum]=1;
        sum=0;
    }
    cout<<mp.size()<<‘\n‘;
    for(auto it=mp.begin();it!=mp.end();){
        cout<<it->first;//每次都不知道这里的空格该怎么控制?!!!
        if(++it!=mp.end())
            cout<<" ";
    }
    return 0;
}

//还是比较简单的,就是用map;

在解决最后输出格式的问题上,可以使用一个if判断,注意应该是++it去判断,而不是it++,深刻地理解了这个前+和后+的区别。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9841548.html

时间: 2024-07-30 16:00:35

PAT 1120 Friend Numbers[简单]的相关文章

1120 Friend Numbers (20 分)

1120 Friend Numbers (20 分) 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 I

PAT 甲级 1120 Friend Numbers

1 #include <iostream> 2 #include <cstring> 3 int a[10000]; 4 int max1=0, n=0; 5 int first = 0; 6 using namespace std; 7 8 void bijiao() 9 { 10 int i; 11 for (i=1;i<=max1; i++) 12 { 13 if (a[i]>=1) 14 { 15 n++; 16 } 17 } 18 } 19 void shuc

PAT 1121 Damn Single[简单]

1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of. Input Specification: Each input file contains one tes

Timus Online Judge1009---K-based Numbers(简单递推dp)

Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn't contain two successive zeros. For example: 1010230 is a valid 7-digit number; 1000198 is not a valid number; 0001235 is not a

1120 Friend Numbers (20)

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

1120 Friend Numbers

题目大意是求一些数字的各位和,看一下有多少不同的,并且按升序输出,水题- #include <iostream> #include <cstring> #include <string> #include <sstream> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <map>

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI