最小的k个数

/*输入n个整数,找出其中最小的k个数,例如输入4,5,1,6,2,7,3,8这8个数字,则最小的四个数字是1,2,3,4*/
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef multiset<int, greater<int> > intSet;
typedef multiset<int, greater<int> > :: iterator SetIterator;
void GetLeastkNum(const vector<int> &data, intSet &LeastNum, int k)
{
    if(k < 1 || data.size() < k)
    return;
    vector<int> :: const_iterator iter = data.begin();
    for(; iter != data.end(); ++iter)
    {
        if(LeastNum.size() < k)
        {
            LeastNum.insert(*iter);
        }
        else
        {
            if(*iter < *(LeastNum.begin()))
            {
                SetIterator iterGreast = LeastNum.begin();
                LeastNum.erase(iterGreast);
                LeastNum.insert(*iter);
            }
        }
    }

}
//===============测试代码===========
void Test(char *testname, int *data, int n, int k, int *expectedresult)
{
    if(testname != NULL)
    cout << testname << " begins:" << endl;
    vector<int> ivec;
    for(int i = 0; i < n; ++i)
    ivec.push_back(data[i]);
    if(expectedresult == NULL)
    cout << "The input is invalid, we don't expect any result." << endl;
    else
    {
        for(int i = 0; i < k; ++i)
        cout << expectedresult[i] << " ";
        cout << endl;
    }
    intSet LeastNums;
    GetLeastkNum(ivec, LeastNums, k);
    for(SetIterator iter = LeastNums.begin(); iter != LeastNums.end(); ++iter)
    cout << *iter << " ";
    cout << endl;
}
//==========测试用例=========
//k小于数组长度
void Test1()
{
    int data[] = {4, 5, 1, 6, 2, 7, 3 ,8};
    int expectedresult[] = {1, 2, 3, 4};
    Test("Test1", data, sizeof(data) / sizeof(int), sizeof(expectedresult) / sizeof(int), expectedresult);
}
//k等于数组长度
void Test2()
{
    int data[] = {4, 5, 1, 6, 2, 7, 3 ,8};
    int expectedresult[] = {1, 2, 3, 4, 5, 6, 7, 8};
    Test("Test2", data, sizeof(data) / sizeof(int), sizeof(expectedresult) / sizeof(int), expectedresult);
}
//k大于数组的长度
void Test3()
{
    int data[] = {4, 5, 1, 6, 2, 7, 3 ,8};
    int *expectedresult = NULL;
    Test("Test2", data, sizeof(data) / sizeof(int), 10, expectedresult);
}
//k等于1
void Test4()
{
    int data[] = {4, 5, 1, 6, 2, 7, 3, 8};
    int expectedresult[] = {1};
    Test("Test4", data, sizeof(data) / sizeof(int), sizeof(expectedresult) / sizeof(int), expectedresult);
}
//k等于0
void Test5()
{
    int data[] = {4, 5, 1, 6, 2, 7, 3 ,8};
    int *expectedresult = NULL;
    Test("Test2", data, sizeof(data) / sizeof(int), 0, expectedresult);
}
//数组中有相同的数字
void Test6()
{
    int data[] = {4, 5, 1, 6, 2, 7, 2, 8};
    int expectedresult[] = {1, 2};
    Test("Test6", data, sizeof(data) / sizeof(int), sizeof(expectedresult) / sizeof(int), expectedresult);
}
//输入空指针
void Test7()
{
    int *expectedresult = NULL;
    Test("Test6", NULL, 0, 0, expectedresult);
}
int main()
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值