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 friend ID's among them. Note: a number is considered a friend of itself.

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

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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int hashTB[] = {,};
string str;
void str2num(string ss){
int len = ss.length();
int ans = ;
for(int i = ; i < len; i++){
ans += (ss[i] - '');
}
hashTB[ans]++;
}
int main(){
int N, cnt = ;
cin >> N;
for(int i = ; i < N; i++){
cin >> str;
str2num(str);
}
for(int i = ; i < ; i++){
if(hashTB[i] > )
cnt++;
}
printf("%d\n", cnt);
int pt = ;
for(int i = ; i < ; i++){
if(hashTB[i] > ){
pt++;
if(pt == cnt)
printf("%d", i);
else printf("%d ", i);
}
}
cin >> N;
return ;
}

总结:

1、注意不要理解错,一个数也可以是自己的朋友。所以一个数组成的和也算在输出范围内。

A1120. Friend Numbers的更多相关文章

  1. PAT A1120 Friend Numbers (20 分)——set

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  2. PAT甲级——A1120 Friend Numbers【20】

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  3. PAT_A1120#Friend Numbers

    Source: PAT A1120 Friend Numbers (20 分) Description: Two integers are called "friend numbers&qu ...

  4. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  5. 1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

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

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

  7. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

  9. [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 ...

随机推荐

  1. Java 基础类型 默认值

    (1)数据库里的列,如果有默认值,不能赋值有业务含义的值. (2)int 默认值 java会分配默认值的额.

  2. 表单中input name属性有无[]的区别

    1 input数组 如下一个表单: <input type="text" name="username[]" value="Jason" ...

  3. poj-2337(欧拉回路输出)

    题意:给你n个字符串,每个字符串可以和另一个字符串连接的前提是,前一个字符串的尾字符等于后一个字符串的首字符,问你存不存在欧拉通路并输出 解题思路:基本标准流程,建图:把一个字符串可以看作一条首字符指 ...

  4. Nginx TSL/SSL优化握手性能

    L:131

  5. django-admin和manage.py

    目录 一.Django内置命令选项 check dbshell diffsettings flush makemigrations migrate runserver shell startapp s ...

  6. 修改iptables后重启返回错误

    在防火墙添加规则后我是这样改的vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ...

  7. linux拷贝多个目录下的文件到同一个目录

    拷贝a目录下的a.txt和b目录下的b.txt到c目录 cp -a \ /root/a/a.txt \ /root/b/b.txt \ /root/c/

  8. BZOJ4514[Sdoi2016]数字配对——最大费用最大流

    题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...

  9. 图灵机器人API接口

    调用图灵API接口实现人机交互 流程一: 注册 图灵机器人官网: http://www.tuling123.com/ 第一步: 先注册, 然后创建机器人, 拿到一个32位的key 编码方式 UTF-8 ...

  10. CODEFORCES掉RATING记 #2

    比赛:Codeforces Round #425 (Div. 2) 时间:2017.7.25晚 先orz zjt rank4 一场加300rating A:傻题,判断\(\lfloor\frac{n} ...