【链家笔试题】问卷调查
程序员文章站
2022-05-05 18:28:19
...
思路
通过Java的TreeSet集合实现去重加排序的效果。
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
TreeSet<Integer> set=new TreeSet<>();
for(int i=0;i<n;i++)
{
set.add(sc.nextInt());
}
System.out.println(set.size());
Iterator<Integer> it=set.iterator();
StringBuilder stringBuilder=new StringBuilder();
while(it.hasNext())
{
stringBuilder.append(it.next()+" ");
}
if(stringBuilder.length()>1)
stringBuilder.deleteCharAt(stringBuilder.length()-1);
System.out.println(stringBuilder.toString());
}
}
推荐阅读