题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
#include <stdio.h> #include <stdlib.h> int compare(const void* a, const void* b) { const int* inta = (const int*)a; const int* intb = (const int*)b; return *inta - *intb; } int main() { int n, k; while (scanf("%d %d", &n, &k) != EOF) { int arr[n]; for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); } qsort(arr, n, sizeof(int), compare); for(int i = 0; i < k - 1; i++) { printf("%d ", arr[i]); } printf("%d\n", arr[k - 1]); } return 0; }