程序設計與算法(三)期末考試之011:編程填空:前K大的偶數

總時間限制: 

1000ms

 

內存限制: 

65536kB

// 在此處補充你的代碼

描述

輸入n個整數,輸出整數數列中大小排名前k的偶數

 

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
#include <set>

using namespace std;
class MyQueue
{
};
int main()
{
	int t;
	cin >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		MyQueue q(k);
		for (int i = 0; i < n; ++i)
			cin >> q;
		cout<<q;
		cout << endl;
	}
	return 0; 
}

輸入

有多組數據
第一行是數據組數 t
對每組數據: 
第一行爲整數n (n>=3)和k
接下來的一行爲n個整數,保證這些整數中至少有k個偶數。

輸出

對每組數據,輸出k個整數,降序排列,表示選出來的大小排名前k的偶數

樣例輸入

2
9 4
1 2 4 3 6 6 7 8 9
3 2
18 16 14

樣例輸出

8 6 6 4
18 16
// 在此處補充你的代碼	
public:
    int k;
    multiset<int, greater<int>> que;
    MyQueue(int _k):k(_k){}
    friend istream & operator>>(istream&is, MyQueue &a) {
        int l;
        is >> l;
        if (l % 2 == 0)a.que.insert(l);
        return is;
    }
    friend ostream & operator <<(ostream&os, MyQueue &a) {
        multiset<int>::iterator p=a.que.begin();
        int count = 0;
        for (;count<=a.k-1; p++) {
            if (count)os << " ";
            os << *p ;
            count++;
        }
        return os;
	}
//

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章