秋天到了,小朋友们去采桔子,他们想把采回来的桔子按重量排排队,已知他们采的桔子重量都是整数,都在1-100克之间,现在请你帮他们按照重量是奇数就从大到小、是偶数从小到大排队输出。
注意:每个桔子的重量可能相同。
输入
共两行。
第一行是桔子的数量n。(1≤n≤200)
第二行是n个正整数,表示这些桔子的重量。
输出
共n行,每行一个整数,表示这些桔子重量如果是奇数就从大到小、如果是偶数就从小到大的顺序。
样例
输入 复制
5
49 50 47 48 48
输出 复制
49
47
48
48
50
那还不简单,用STL的sort函数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> weights(n);
for (int i = 0; i < n; i++)
cin >> weights[i];
sort(weights.begin(), weights.end(), [](auto w1, auto w2)
{
if (w1 % 2 == 0)
{
if (w2 % 2 == 0)
return w1 < w2;
else
return false;
}
else
{
if (w2 % 2 == 0)
return true;
else
return w1 > w2;
}
});
for (auto w : weights)
cout << w << '\n';
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
5
49 50 47 48 51
51
49
47
48
50
这代码不对啊,c++运行不了
#include<bits/stdc++.h>
using namespace std;
int n,a[210];
double cure(int p){
double res=0;
for(int i=0;i<n;i++){
if(i!=p)res+=a[i];
}
return res/(n-2);
}
int main(){
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n); //排序
printf("%.1lf\n",cure(0)); //去掉最大的橘子
for(int i=1;i<n-1;i++)cout<<a[i]<<" "; //去掉最大、最小的橘子
cout<<endl;
return 0;
}
```c++
```