找出n个数里最小的k个

找出n个数里最小的k个

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int cmpfun(const void *a,const void *b)
 6 {
 7     return (*(int *)a - *(int *)b);
 8 }
 9 
10 int main(int argc,char *argv)
11 {
12 //思路:排序,然后输出最小的k个值
13 //array:输入的数组 str:用来判断是否结束 count:数组长度
14     int array[100];
15     int k,i=0,count=0;
16     char str=' ';
17     while(str!='\n')
18     {
19         scanf("%d",&array[i++]);
20         str=getchar();
21         count++;
22     }
23 
24     {
25         k=array[--count];
26     }
27     qsort(array,count,sizeof(int),cmpfun);
28 
29     for(i=0;i<k;i++)
30         printf("%d  ",array[i]);
31 
32     return 0;
33 }

 

posted @ 2018-03-30 15:00  第五  阅读(264)  评论(0编辑  收藏  举报