PAT——1064. 朋友数(set用法)

如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”。例如123和51就是朋友数,因为1+2+3 = 5+1 = 6,而6就是它们的朋友证号。给定一些整数,要求你统计一下它们中有多少个不同的朋友证号。注意:我们默认一个整数自己是自己的朋友。

输入格式:

输入第一行给出正整数N。随后一行给出N个正整数,数字间以空格分隔。题目保证所有数字小于104

输出格式:

首先第一行输出给定数字中不同的朋友证号的个数;随后一行按递增顺序输出这些朋友证号,数字间隔一个空格,且行末不得有多余空格。

输入样例:

8
123 899 51 998 27 33 36 12

输出样例:

4
3 6 9 26


首先回顾一下题目,题目的要求是 有序,并且不重复 的输出元素,这个与set的特征完全吻合。

 1 package com.hone.basical;
 2 
 3 import java.util.Scanner;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6 /**
 7  * 原题目:https://www.patest.cn/contests/pat-b-practise/1064
 8  * @author Xia
 9  * 朋友数
10  * 上一个利用list来存储,现在利用set来存储
11  * 因为set是不重复,并且有序的。
12  */
13 
14 public class basicalLevel1064friends2Set {
15 
16     public static void main(String[] args) {
17         Scanner in = new Scanner(System.in);
18         int n = in.nextInt();
19         Set<Integer> sum = new TreeSet<>();
20         for (int i = 0; i < n; i++) {
21             int a = in.nextInt();
22             int b = 0;
23             while (a>0) {
24                 b +=a%10;
25                 a = a/10;
26             }
27             sum.add(b);
28         }
29         System.out.println(sum.size());
30         boolean isFirst = true;
31         for (Integer id : sum) {
32             if (!isFirst) {
33                 System.out.print(" ");
34             }
35             System.out.print(id);
36             isFirst = false;
37         }
38     }
39 }

 

------------------------Set用法-------------------------------------------------------

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such
that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical
set abstraction.

引用java Api中的话,Set实际上就是数学上集合的抽象,要求其不能有相同的元素,并且最多只能有一个空元素。

下面用具体的代码来表示其方法

 1 package com.hone.test;
 2 
 3 import java.util.Iterator;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6 /**
 7  * Set 
 8  * TreeSet类实现了SortedSet接口,能够对集合中的对象进行排序。
 9  * @author Xia
10  */
11 public class Sets {
12     public static void main(String[] args) {
13         Set<Integer> test = new TreeSet<>();
14         
15         Set<Integer> needAdd = new TreeSet<>();
16         int a = 1;
17         int b = 2;
18         int c = 3;
19         int h = 7;
20         int w = 5;
21         test.add(a);
22         test.add(b);
23         needAdd.add(h);
24         needAdd.add(w);
25         System.out.println(test.size());            //判断容器的大戏i奥
26         System.out.println(test.isEmpty());            //判断容器是否为空
27         System.out.println(test.remove(a));            //移除容器中的a元素
28         test.add(c);
29         //利用foreach遍历
30         for (Integer value : test) {
31             System.out.print(value+" ");
32         }
33         System.out.println();
34         
35         //利用Iterator实现遍历
36         Iterator<Integer> value = test.iterator();
37         while (value.hasNext()) {
38             int s = value.next();
39             System.out.print(s+" ");
40         }
41         System.out.println();
42         
43         //将一个集合添加到另一个集合中
44         test.addAll(needAdd);
45         for (Integer value2 : test) {
46             System.out.print(value2+" ");
47         }
48         System.out.println();
49         
50         //删除集合,并且利用foreach重新遍历
51         System.out.println(test.removeAll(needAdd));
52         for (Integer value2 : test) {
53             System.out.print(value2+" ");
54         }
55         System.out.println();
56         
57         //clear()清空集合中的元素
58         needAdd.clear();
59         System.out.println(needAdd.size());
60     }
61 }

 

posted @ 2017-11-18 14:47  SnailsCoffee  阅读(329)  评论(0编辑  收藏  举报