A1120. Friend Numbers
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的更多相关文章
- PAT A1120 Friend Numbers (20 分)——set
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- PAT甲级——A1120 Friend Numbers【20】
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- PAT_A1120#Friend Numbers
Source: PAT A1120 Friend Numbers (20 分) Description: Two integers are called "friend numbers&qu ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- 1120 Friend Numbers (20 分)
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [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 ...
随机推荐
- Dart语法基础
hello world // Define a function. printNumber(num aNumber) { print('The number is $aNumber.'); // Pr ...
- java中级——集合框架【1】-ArrayList
集合框架----ArrayList 引子:我们先来看看传统数组的用法 写一个Hero对象类 package cn.jse.t1; public class Hero { public String n ...
- 2017 Python最新面试题及答案16道题
1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...
- Django--ORM和单表查询
一 . ORM ORM是“对象-关系-映射”的简称.(Object Relational Mapping,简称ORM) 二. 单表操作 要想将模型转为mysql数据库中的表,需要在setting里面写 ...
- shell中的>,2>&1,&>file 解析记录
0 表示标准输入1 表示标准输出2 表示标准错误输出> 默认为标准输出重定向,与 1> 相同2>&1 意思是把 标准错误输出 重定向到 标准输出.&> ...
- Java开发之@PostConstruct执行顺序
构造函数==>postConstruct==>init==destory==>predestory==卸载servlet;; 从Java EE5规范开始,Servlet增加了两个影响 ...
- 老男孩python学习自修第十天【三元表达式与lambda表达式】
例如: 1.使用三元表达式给变量赋值 result = '空' if x == None else x 2.使用lambda定义函数 add = lambda x, y: x+y
- E: Unable to correct problems, you have held broken packages
问题: apt install libmysqlclient-dev Reading package lists... DoneBuilding dependency tree Readi ...
- fiddler 学习笔记1-下载安装、开启、关闭抓包功能
1 下载安装(安装于C盘之外的空间中) https://www.telerik.com/fiddler 2 开启抓包功能:安装后默认为开启状态 点击 file-capture 或左下角capture ...
- 本科理工男如何学习Linux
我是一个本科学电子的理工男,但是一直对计算机感兴趣,所以平时自己在课下喜欢学一些与计算机有关的东西.由于对计算机感兴趣,所以后来我参加了学校的计算机社团,在那里接受一些培训和指导.当时在社团里看到师兄 ...