7.29 第二题
public class Test { public static void setAdd(Set<Integer> set,Integer i){ if(!set.add(i)){
set.remove(i); setAdd(set,i<<1); }
} public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Integer[] array = new Integer[n]; for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt(); }
LinkedHashSet<Integer> res = new LinkedHashSet<>(); for (int i = 0; i < array.length; i++) { setAdd(res,array[i]); }
Iterator<Integer> iterator = res.iterator(); while (iterator.hasNext()){
System.out.print(iterator.next()); if(iterator.hasNext())System.out.print(" "); }
}
}
全部评论
(5) 回帖