代码如下:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int num = input.nextInt(); ArrayList<Integer> arrList = new ArrayList<Integer>(); for(int i = 0;i<num;i++){ arrList.add(input.nextInt()); } ArrayList<Integer> arrListTemp = new ArrayList<Integer>(arrList); for(int j = 0;j<num;j++){ int temp = arrListTemp.get(j); while(temp!=1){ if(temp%2==1){ temp = (temp*3+1)>>1; if(arrList.contains(temp)){ arrList.remove(Integer.valueOf(temp)); } }else{ temp = temp>>1; if(arrList.contains(temp)){ arrList.remove(Integer.valueOf(temp)); } } } } Collections.sort(arrList,Collections.reverseOrder()); for(int k = 0;k<arrList.size();k++){ if(k==(arrList.size()-1)){ System.out.print(arrList.get(k)); }else{ System.out.print(arrList.get(k)+" "); } } } }