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. spring AOP源码分析(二)

    现在,我们将对代理对象的生成过程进行分析. 在springAOP源码分析(一)的例子中,将会生成哪些对象呢? 可以看到将会生成六个对象,对应的beanName分别是: userDao:目标对象 log ...

  2. Angular ngRepea

    <!DOCTYPE html><html ng-app><head lang="en"> <meta charset="UTF- ...

  3. java实现计算器

    Main_business.java import java.util.Scanner; public class Main_business { public void getMyCalculate ...

  4. 如何确定 Hadoop map和reduce的个数--map和reduce数量之间的关系是什么?

    1.map和reduce的数量过多会导致什么情况?2.Reduce可以通过什么设置来增加任务个数?3.一个task的map数量由谁来决定?4.一个task的reduce数量由谁来决定? 一般情况下,在 ...

  5. Object.defineProperties()与Proxy对象代理

    Object.defineProperties() 了不起啊..vue.js通过它实现双向绑定的 Object.defineProperties(obj,props) 方法直接在一个对象上定义新的属性 ...

  6. VMware与CentOS的安装与Linux简单指令

    一 . VMware与CentOS系统安装 下载CentOS系统的ISO镜像 # 官方网站,国外网站,下载速度会很慢 www.centos.org # 由于国外的下载速度慢,我们可以使用国内的镜像源 ...

  7. Windows 10 安装PHP Manager 失败的解决办法

    首先安装.NET 2.0和.NET 3.5, 在  控制面板----程序----启用或关闭Windows功能   里面 然后修改注册表:HKLM/System/CCS/Services/W3SVC/P ...

  8. 6.docker的私用镜像仓库registry

    docker方式启动镜像仓库 / # cat /etc/docker/registry/config.yml version: 0.1 log: fields: service: registry s ...

  9. HttpWebRequest using Basic authentication

    System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( ...

  10. codeforces433B

    Kuriyama Mirai's Stones CodeForces - 433B 有n颗宝石,每个宝石都有自己的价值. 然后m次询问.问区间[i,j]的宝石的总值,或者问排序后的区间[i,j]的总值 ...