思路:先创建三个数组,一个数组负责读入,另外两个数组一个存奇数,一个存偶数。并将存奇数的数组降序排序,偶数用升序排序。排序的函数用 sort 即可。排完序后用根据读入数组的奇偶性输出两个数组即可。 代码:
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005], c[100005];
int main()
{
int t, s = 0;
cin >> t;
while (t--)
{
s++;
int n, cnt1 = 0, cnt2 = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] % 2)
{
cnt1++;
b[cnt1] = a[i];
}
else
{
cnt2++;
c[cnt2] = a[i];
}
}
sort(b + 1, b + 1 + cnt1, greater<int>());
sort(c + 1, c + 1 + cnt2);
cout << "Case #" << s << ":";
for (int i = 1; i <= n; i++)
{
if (a[i] % 2) cout << " " << b[cnt1--];
else cout << " " << c[cnt2--];
}
cout << "\n";
}
return 0;
}