/*
Find the kth smallest element in a sorted array
*/
#include <string>
#include <iostream>
int main()
{
int a[10] = {1,2,2,2,3,3,4,5,6,7};
int n = 10;
int count = 1;
int i = 0;
int k;
std::cin >> k;
while(i < n)
{
if(a[i] != a[i + 1])
{
if(count == k)
{
std::cout << k << "smallest number" << a[i];
}
count++;
}
i++;
}
return 0;
}微软面试题 07022012 [1]
最新推荐文章于 2025-12-11 16:19:54 发布
本文介绍了一种算法来找出一个已排序数组中第k小的元素,通过遍历数组并计数,直到找到目标元素。
814

被折叠的 条评论
为什么被折叠?



