11321 - Sort! Sort!! and Sort!!!

mail@pastecode.io avatar
unknown
java
2 years ago
1.1 kB
2
Indexable
Never
class Number implements Comparable<Number> 
{
	int num;
	int mod;
	int isOdd;

	public void set(int num, int mod) {
		this.num = num;
		this.mod = num % mod;
		this.isOdd = (num % 2 == -1 || num % 2 == 1) ? 1 : 0;
	}

	@Override
	public int compareTo(Number x) {
		if (this.mod == x.mod) {
			if (this.isOdd == x.isOdd) {
				if (this.isOdd == 1)
					return x.num - this.num;
				else
					return this.num - x.num;

			} else
				return x.isOdd - this.isOdd;

		} else
			return this.mod - x.mod;
	}
}

public class Main {
	final static int MAX = 10000;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Number[] arr = new Number[MAX];
		for (int i = 0; i < MAX; i++)
			arr[i] = new Number();
		while (sc.hasNext()) {
			int N = sc.nextInt();
			int M = sc.nextInt();
			System.out.println(N + " " + M);
			if (N == 0 && M == 0)
				break;
			for (int i = 0; i < N; i++)
				arr[i].set(sc.nextInt(), M);
			Arrays.sort(arr, 0, N);
			for (int i = 0; i < N; i++)
				System.out.println(arr[i].num);

		}
	}
}