题解 | #整数奇偶排序#
整数奇偶排序
https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { // int a, b; vector<int> ji; vector<int> ou; int a[10]; while (cin >>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]) { // 注意 while 处理多个 case // cout << a + b << endl; sort(a,a+10); //先行排序 for (int i = 0; i <= 9; i++) { if (a[i] % 2 == 0 ) { ou.push_back(a[i]); //偶数扔进偶数vector } if(a[i] % 2 != 0 && a[i] != 0 ) { ji.push_back(a[i]); //基数扔进奇数vector,此时,奇偶序列均已有序,由小到大 } } for(int i = ji.size() - 1;i >= 0;i--) //按题目要求输出 { cout<< ji[i] << " " ; } for(int i = 0;i <= ou.size() - 1;i++) //按题目要求输出 { cout << ou[i] << " "; } } return 0; } // 64 位输出请用 printf("%lld")