PAT A1120 Friend Numbers (20 分)——set
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 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 <stdio.h>
#include <set>
using namespace std;
int main(){
int n;
scanf("%d",&n);
set<int> s;
for(int i=;i<n;i++){
int tmp;
scanf("%d",&tmp);
int num=;
while(tmp!=){
num+=tmp%;
tmp/=;
}
s.insert(num);
}
int cnt=;
printf("%d\n",s.size());
for(auto it=s.begin();it!=s.end();it++){
printf("%d",*it);
cnt++;
if(cnt!=s.size())printf(" ");
}
}
注意点:用set很方便,但set的指针迭代器只有加法操作没有减法操作,所以判断输出空格不能靠迭代器指针,还是要另外加一个变量
PAT A1120 Friend Numbers (20 分)——set的更多相关文章
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)
1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits ...
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- 1023 Have Fun with Numbers (20 分)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- pat 1035 Password(20 分)
1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...
- pat 1008 Elevator(20 分)
1008 Elevator(20 分) The highest building in our city has only one elevator. A request list is made u ...
- PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)
1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT 1042 Shuffling Machine (20 分)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
随机推荐
- Confluence设置MySQL数据库报错:必须使用'READ-COMMITTED'作为默认隔离级别。
解决方案: mysql -u root -p123456 SET GLOBAL tx_isolation='READ-COMMITTED'; mysql数据库创建 1.设置mysql隔离级别 SET ...
- LintCode Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. Have you met this question in a ...
- php 对象转数组
//参考网上 但是别人给的方法有错误的地方public function eleme_callback(){ $res = (object) array('1' => 'foo'); $data ...
- 从gitHub上拉取并运行项目
今天我们来试一下如何从gitHub上拉取一个项目并且运行起来,话不多说,我们直接开搞可好 1.首先我们先获取到项目地址(此处我以自己的项目地址作为示例) 我们选择红圈处的clone or downlo ...
- 纯小白入手 vue3.0 CLI - 2.7 - 组件之间的数据流
vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 尽量把纷繁的知识,肢解重组成为可以堆砌的知识. ...
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix
https://www.jianshu.com/p/e1c8270477bc?utm_campaign=maleskine&utm_content=note&utm_medium=se ...
- spring cloud 配置文件application.yml和bootstrap.yml 的定位,区别和联系总算是有一点明白了
最近在启用springcloud配置中心server的东西,在整理属性资源的时候,突然发现:用了这么久的springboot,为什么会配置两个属性文件同时存在(application.yml/prop ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- Source Insight 查找的选择项
查找参数:whole words only : 全字匹配查找case sensitive : 区分大小写project wide ...
- SQL SERVER利用BCP命令在命令行下导出数据到csv文件中
bcp "select * from (DBNAME).dbo.qt_trace where User_1 is not null" queryout c:\%date:~6,4% ...