题解 | #输入n个整数,输出其中最小的k个#

输入n个整数,输出其中最小的k个

http://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c

暴力硬排,能过就行

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()) {
            String num = sc.nextLine();
            String[] numSpilt = num.split(" ");
            sort(numSpilt);

            int needNum = Integer.valueOf(numSpilt[0]);

            String collectionStr = sc.nextLine();
            String[] collectionSpilt = collectionStr.split(" ");
            sort(collectionSpilt);

            String s = "";
            for(int x = 0;x<needNum;x++){
                if(x < (needNum -1)){
                    s += collectionSpilt[x] + " ";
                } else{
                    s += collectionSpilt[x];
                }
            }
            System.out.println(s);
        }
        
    }
    
    public static void sort(String[] args){
        for(int y = 0;y < args.length - 1;y++){
            for(int x = 0;x < args.length - 1;x++){
                if(Integer.valueOf(args[x]) > Integer.valueOf(args[x+1])){
                    String s = args[x];
                    args[x] = args[x+1];
                    args[x + 1]=s;
                }
            }
        }
    }
}
全部评论

相关推荐

03-23 23:00
黄淮学院 Java
才浅Caiq:老家县城送外卖也5000,要求别这么低
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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