Source:

PAT A1120 Friend Numbers (20 分)

Description:

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 1.

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

Keys:

  • 简单模拟

Code:

 /*
Data: 2019-08-14 16:21:28
Problem: PAT_A1120#Friend Numbers
AC: 13:24 题目大意:
数字的各位和称为朋友ID,求有多少个朋友ID
*/
#include<cstdio>
#include<set>
#include<string>
#include<iostream>
using namespace std; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,cnt=;
string s;
set<int> st;
scanf("%d", &n);
for(int i=; i<n; i++)
{
cin >> s;
cnt=;
for(int j=; j<s.size(); j++)
cnt += s[j]-'';
st.insert(cnt);
}
cnt=;
printf("%d\n", st.size());
for(auto it=st.begin(); it!=st.end(); it++)
printf("%d%c", *it,++cnt==st.size()?'\n':' '); return ;
}

PAT_A1120#Friend Numbers的更多相关文章

  1. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. HDU 4525

    也是水题了,不过注意负负也可以为正就好了. 今天看见bestcoder上的人那么厉害,唉,我什么时候才能赶上啊.. #include <iostream> #include <cst ...

  2. 基数排序之多keyword排序运用队列

    源码例如以下: #include <stdlib.h> #include <stdio.h> typedef struct QUEUEnode* link; struct QU ...

  3. hdu5242 上海邀请赛 优先队列+贪心

    题意是给你一棵树    n个点 n-1条边   起点是1   每一个点都有权值 每次能从根节点走到叶子节点   经行k次游戏 每次都是从1開始    拿过的点的权值不能拿第二次   问最大权值和. 開 ...

  4. ubuntu下7z文件的解压方法

    apt-get install p7zip-full 控制台会打出以下信息: 正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       建议安装的 ...

  5. C++顺序表(模板总结)

    C++顺序表(模板总结) 总结: 1.模板类的实质是什么:让程序员写出和类型无关的代码 2.模板的对象时什么:方法或者类 3.是对类中的一系列操作,提供一个不固定数据类型的方法 用模板做的类的时候要指 ...

  6. PCB Genesis增加轮廓字 实现原理

    在Genesis增加汉字自带是不支持增加汉字的,如果需增加汉字需用到CAD 汉字库才可增加汉字,这里介绍一种脱离汉字库实现 Genesis增加轮廓字(如要变为实体,填空Surface可变为实体字) 一 ...

  7. PCB MS SQL 通过表名查询各字段信息和vb.net C# module类代码

    正式表:各字段内容获取 ) SET @tabname = 'ppeflow' SELECT @tabname AS '表名' ,(CASE ))+ ')' )) ) )) + ')' )) ) )) ...

  8. 昂贵的聘礼(Dijkstra)

    http://poj.org/problem?id=1062 每个物品看成一个节点,酋长的允诺也看作一个物品, 如果一个物品加上金币可以交换另一个物品,则这两个节点之间有边,权值为金币数,求第一个节点 ...

  9. netty之ByteBuf详解

    [ChannelPromise作用:可以设置success或failure 是为了通知ChannelFutureListener]Netty的数据处理API通过两个组件暴露——abstract cla ...

  10. Android检测代理

    1. System.getProperties().remove("http.proxyHost"); System.getProperties().remove("ht ...