题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#

输入整型数组和排序标识,对其元素按照升序或降序进行排序

http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309

import java.util.*;
public class Main {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                int n = sc.nextInt();
               Integer[] arr = new Integer[n];
                for(int i =0;i<n;i++){
                    arr[i]=sc.nextInt();
                }
                int flag = sc.nextInt();
                if(flag==0){
                    //升序
                    Arrays.sort(arr,new Comparator<Integer>(){
                        public int compare(Integer o1,Integer o2){
                            return o1-o2;
                        }
                    });
                }else if(flag ==1){
                    //降序
                    Arrays.sort(arr,new Comparator<Integer>(){
                        public int compare(Integer o1,Integer o2){
                            return o2-o1;
                        }
                    });
                }
                for(Integer m : arr){
                    System.out.print(m+" ");
                }
                System.out.println();
            }
        }
}
全部评论

相关推荐

10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
牛客21331815...:像我一投就pass,根本不用焦虑泡池子
点赞 评论 收藏
分享
评论
3
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务