链接
http://ac.jobdu.com/problem.php?pid=1174
-
题目描述:
-
查找一个数组的第K小的数,注意同样大小算一样大。
如 2 1 3 4 5 2 第三小数为3。
-
输入:
-
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000),再输入k。
-
输出:
-
输出第k小的整数。
-
样例输入:
-
6 2 1 3 5 2 2 3
-
样例输出:
-
3
题意:输出数组中第k小的数字
分析:先快排,再遍历查找第k小(有相等数字的要考虑)
注意:sort使用的比较函数cmp里不要使用!=、<=的比较,要使用<、>。切记,切记
ac代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int a[1000];
int T,t;
while(cin>>T)
{
for(int i=0;i<T;i++)
scanf("%d",&a[i]);
cin >> t;
sort(a,a+T);
int counter = 1;
for(int i=0;i<T-1;i++)
{
if(a[i+1]>a[i])
counter++;
if(counter==t){
printf("%d\n",a[i+1]);
break;
}
}
}
return 0;
}