题解 | #输入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;
}
}
}
}
}