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 ...
随机推荐
- Eclipse在写java时的BUG
要把这个关掉
- 获取打开页面时的当前时间(yyyy-MM-dd hh:mm:ss)
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...
- liunx安装nginx
参考 https://blog.csdn.net/dyllove98/article/details/41120789 1,去官网下载最新的包 官网地址:http://nginx.org/downlo ...
- SQL之CASE WHEN用法详解[1]
简单CASE WHEN函数: CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END CASE SCORE WHEN 'B' THEN '良' ELSE '不及格' E ...
- nginx反向代理(动静分离)
使用反向代理(动静分离)可以让nginx专注静态内容,把动态请求交给apache来处理,发挥各自的优势,而且整个架构更加清晰: 这里假设你已经搭建好了nginx环境; 为了简单起见,就不用源码编译安装 ...
- 只要访问url地址 那么容器就会根据地址进行对象的创建
只要访问url地址 那么容器就会根据地址进行对象的创建
- 清北学堂(2019 4 28 ) part 2
主要内容数据结构: 1.二叉搜索树 一棵二叉树,对于包括根节点在内的节点,所有该节点左儿子比此节点小,所有该节点右儿子比该节点大,(感觉好像二分...) 每个节点包含一个指向父亲的指针,和两个指向儿子 ...
- C# MD5,hmacSHA1
一 MD5 推荐使用: md5 MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.G ...
- Spring01-Ioc基本使用
一. Spring简介 1. Spring介绍 Spring框架主页: Spring官网 Spring资源地址:下载地址 Spring框架,由Rod Johnson开发 Spring是一个非常活跃的开 ...
- idea maven +spring mvc
1.步骤一 2.目录结构 3.maven <!--测试--> <dependency> <groupId>junit</groupId> <art ...