跳到正文

题解

题解:P16468 [GKS 2013 #A] Sorting

afo_subway约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年5月17日
更新
2026年5月17日
篇幅
约 1 分钟
题目
P16468

版本

  1. v2当前
  2. v1

思路:先创建三个数组,一个数组负责读入,另外两个数组一个存奇数,一个存偶数。并将存奇数的数组降序排序,偶数用升序排序。排序的函数用 sort 即可。排完序后用根据读入数组的奇偶性输出两个数组即可。 代码:

C++
#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;
}
原文