56 lines
1.1 KiB
C
56 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int gcd(int a, int b) {
|
|
while (b) {
|
|
int temp = a % b;
|
|
a = b;
|
|
b = temp;
|
|
}
|
|
return a;
|
|
}
|
|
|
|
int compare(const void *a, const void *b) {
|
|
return (*(int*)a - *(int*)b);
|
|
}
|
|
|
|
int main() {
|
|
int n;
|
|
scanf("%d", &n);
|
|
|
|
int ages[n];
|
|
for (int i = 0; i < n; i++) {
|
|
scanf("%d", &ages[i]);
|
|
}
|
|
|
|
// Sort the ages in ascending order.
|
|
qsort(ages, n, sizeof(int), compare);
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
if (gcd(ages[i], ages[i-1]) == 1) {
|
|
int swapped = 0;
|
|
for (int j = i + 1; j < n; j++) {
|
|
if (gcd(ages[j], ages[i-1]) > 1) {
|
|
// Swap ages[i] and ages[j].
|
|
int temp = ages[i];
|
|
ages[i] = ages[j];
|
|
ages[j] = temp;
|
|
swapped = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!swapped) {
|
|
printf("Neibb\n");
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
printf("%d ", ages[i]);
|
|
}
|
|
|
|
return 0;
|
|
}
|