1120. Friend Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

————————————————————————————————

题目的意思是问把n个数,每个数每位加起来的和作为一个值,把不同的值从小到大输出

思路:把每个数的和扔到set里,输出

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int main()
{
int n,x;
scanf("%d",&n);
set<int>s;
for(int i=0; i<n; i++)
{
scanf("%d",&x);
int sum=0;
while(x>0)
{
sum+=x%10;
x/=10;
}
s.insert(sum);
}
printf("%d\n",s.size());
set<int>::iterator it=s.begin();
int q=0;
for(; it!=s.end(); it++)
{
if(q++)
printf(" ");
printf("%d",*it);
}
printf("\n");
return 0;
}

  

PAT甲级 1120. Friend Numbers (20)的更多相关文章

  1. PAT甲级——1100 Mars Numbers (字符串操作、进制转换)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...

  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 (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)

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

  4. PAT甲题题解-1120. Friend Numbers (20)-水题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789775.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  6. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  7. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  8. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  9. PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

    1041 Be Unique (20 分)   Being unique is so important to people on Mars that even their lottery is de ...

随机推荐

  1. Git二进制文件冲突解决

    Git二进制文件冲突解决 在我们合并分支的时候,如果两个分支都进行了修改那么就会产生合并冲突.对于非二进制文件的冲突解决,git会给出冲突的位置我们可以手动修改然后再commit.但是对于非二进制文件 ...

  2. grep如何进行正则表达式查找

    字符类 字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在-这个时候,我可以这样来搜寻: [root@www ~]# grep -n ...

  3. spring batch遇到的一些问题

    1.Spring Batch - A job instance already exists: JobInstanceAlreadyCompleteException 这是因为JobParameter ...

  4. POJ3417 Network

    一道LCA+树上差分 原题链接 显然每一条新增边都会导致环. 如果试着举些例子的话,很容易发现割掉非环上的边,则割掉其他任意一条新增边都可达成目标:若割掉的原有边是一个环上的边,那么只有割掉导致这个环 ...

  5. BZOJ 1874 取石子游戏 - SG函数

    Description $N$堆石子, $M$种取石子的方式, 最后取石子的人赢, 问先手是否必胜 $A_i <= 1000$,$ B_i <= 10$ Solution 由于数据很小, ...

  6. 20172306《java程序设计与数据结构》第六周学习总结

    20172306<Java程序设计>第六周学习总结 教材学习内容总结 第八章关键学习了数组的相关内容.我觉得主要分一下几点: 1.索引是从0开始,要区分好索引值和个数值.0的索引处是第一个 ...

  7. [翻译]Javaslang 介绍

    原文地址:Introduction to Javaslang 1. 概述 在这篇文章中,我们将会探讨: Javaslang 是什么? 为什么需要它? 以及怎样在项目中使用它? Javaslang 是J ...

  8. 在HashTable上下文中,同步指的是什么?

    同步意味着在一个时间点只能有一个线程可以修改hash表,任何线程在执行HashTable的更新操作前都需要获取对象锁,其他线程需要等带锁的释放.

  9. [Jmeter] Concurrency Thread Group

    Concurrency Thread Group : https://jmeter-plugins.org/wiki/ConcurrencyThreadGroup/ 参数介绍: Target Conc ...

  10. 查看PHP代码执行的时间

    $t1 = microtime(true); //...要执行的代码$t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒';