奇升偶降
c++能通过,c wrong
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
while (cin >> n) {
vector<int> odd, even;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
if (i % 2 == 0) {
odd.push_back(x);
}
else {
even.push_back(x);
}
}
sort(odd.begin(), odd.end());
sort(even.begin(), even.end(), greater<int>());
int i = 0, j = 0;
bool first = true;
while (i < odd.size() || j < even.size()) {
if (i < odd.size()) {
if (!first) cout << " ";
cout << odd[i++];
first = false;
}
if (j < even.size()) {
cout << " " << even[j++];
}
}
cout << endl;
}
return 0;
}
#include<stdio.h>
#define N 110
void f1(int s[], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (s[j] >= s[j + 1]) {
int temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
void f2(int s[], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (s[j] <= s[j + 1]) {
int temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
int main() {
int n;
while (scanf("%d", &n) == 1) {
int a[N], b[N];
int la = 0, lb = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (i % 2 == 0) { // 奇数位
a[la++] = x;
}
else { // 偶数位
b[lb++] = x;
}
}
f1(a, la);
f2(b, lb);
int i;
for (i = 0; i < la && i < lb; i++) {
printf("%d %d ", a[i], b[i]);
}
if (la > lb) {
printf("%d ", a[la - 1]);
}
printf("\n");
}
return 0;
}