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