zoukankan      html  css  js  c++  java
  • PAT1005

    代码如下:

    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)+" ");
                }
            }
        }
    }
  • 相关阅读:
    java web 资源文件读取
    页面跳转
    验证码的随机图片
    spring 注解
    回文字符串系列问题
    【leetcode】Find All Anagrams in a String
    斐波那契数列
    【leetcode】 First Missing Positive
    Trapping Rain Water
    区间合并问题
  • 原文地址:https://www.cnblogs.com/LoganChen/p/8638345.html
Copyright © 2011-2022 走看看