文章

27

粉丝

86

获赞

10

访问

21.0k

头像
整数奇偶排序 题解:
P1248 北京大学机试题
发布于2023年8月15日 11:12
阅读数 515

C++

清空vector使用clear()

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	vector<int> a(10);
	vector<int> odd;
	vector<int> even;
	vector<int>::iterator it;
	while(cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9]){
		odd.clear();
		even.clear();
		for(it = a.begin(); it != a.end(); it++){
			if(*it % 2 != 0){
				odd.push_back(*it);
			}
			else{
				even.push_back(*it);
			}
		}
		sort(odd.begin(), odd.end(), greater<int>());
		sort(even.begin(), even.end(), less<int>());
		for(it = odd.begin(); it != odd.end(); it++){
			cout << *it << " ";
		}
		for(it = even.begin(); it != even.end(); it++){
			cout << *it << " ";
		}
		cout << endl;
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发