1120. Friend Numbers (20)-PAT甲级真题
来源:互联网 发布:下载手机app软件 编辑:程序博客网 时间:2024/05/20 05:25
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:
8123 899 51 998 27 33 36 12
Sample Output:
43 6 9 26
import java.util.Scanner;import java.util.Set;import java.util.TreeSet;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int numOfInteger = scanner.nextInt();Set<Integer> set = new TreeSet<Integer>();for (int i = 0; i < numOfInteger; i++) {int temp = scanner.nextInt();set.add(getFriendId(temp));}scanner.close();System.out.println(set.size());boolean isFirst = true;for (int id : set) {if (!isFirst) {System.out.print(" ");}System.out.print(id);isFirst = false;}}private static int getFriendId(int num) {int sum = 0;while(num != 0) {sum += num % 10;num /= 10;}return sum;}}
- 1120. Friend Numbers (20)-PAT甲级真题
- 1120. Friend Numbers (20)-PAT甲级真题
- PAT - 甲级 - 1120. Friend Numbers (20)
- 1120. Friend Numbers (20) PAT 甲级
- PAT甲级 1120. Friend Numbers (20)
- PAT 甲级 1120. Friend Numbers (20)
- 1100. Mars Numbers (20)-PAT甲级真题
- PAT--1120. Friend Numbers (20)
- 【PAT】1120. Friend Numbers
- pat 1120. Friend Numbers
- PAT(A)- 1120. Friend Numbers (20)
- 【PAT】【Advanced Level】1120. Friend Numbers (20)
- 1023. Have Fun with Numbers (20)-PAT甲级真题(大整数运算)
- 1069. The Black Hole of Numbers (20)-PAT甲级真题
- PAT A1120 friend numbers (20)
- PAT A1120. Friend Numbers (20)
- 1008. Elevator (20)-PAT甲级真题
- 1061. Dating (20)-PAT甲级真题
- 数据结构实验之排序六:希尔排序
- 异常:java.lang.IllegalArgumentException: node to traverse cannot be null!
- 51Nod 1035 最长的循环节
- 低耦合,简单易懂
- CAP原则
- 1120. Friend Numbers (20)-PAT甲级真题
- 找试场——世上无难题,只要肯思考_耀晨SYW
- ajax方法的用法
- Linux进程间通信——使用消息队列
- Linux进程间通信——使用共享内存
- 11:字符串、字符数组、字符指针数组
- java学习(synchronized)
- 1064. 朋友数(20)-PAT乙级真题
- android TV遥控器控制GridView不能正常滚动