Try   HackMD

Sort! Sort!! And Sort!!!

題目連結 UVa 11321

中文簡述

按照以下原則排序

  1. 按照除以M的餘數排,越少的在越前面
  2. 如果餘數相同,按照奇偶數排
  3. 偶數越排越大,奇數越排越小

solution:

#include <bits/stdc++.h>
using namespace std;
int n, m;
bool cmp(int i, int j) 
{	
	if (i % m != j % m)
		return i % m < j % m;
	if ((i & 1) && (j & 1))
		 return i > j;
	else if (!(i & 1) && !(j & 1))
		 return i < j;
	else
		return i & 1;
}

int main() 
{
	while (cin >> n >> m, n) 
	{
		cout << n << " " << m << endl;
			vector<int> nums(n);
			for (auto& i : nums)
				cin >> i;
			sort(nums.begin(), nums.end(), cmp);
			for (auto& i : nums)
				cout << i << endl;
	}
	cout << 0 << " " << 0 << endl;
}
tags: UVA

回目錄 學習筆記