{ "question_id": 7404098, "question_title": "一个整数的序列,要求对其重新排序", "question_content": "
一个整数的序列,要求对其重新排序。排序要求:\n1.奇数在前,偶数在后;\n2.奇数按从大到小排序;\n3.偶数按从小到大排序。
\n输入一行,包含整数个数n, n个整数值,彼此以一个空格分开。\n按照要求排序后输出一行,包含排序后的n 个整数。\n
用一个数组也可以的:
\n\n#include <iostream>
\n#include<algorithm>
\nusing namespace std;
bool cmp(int a, int b){
\n int x = a % 2;
\n int y = b % 2;
\n if(x == y)
\n if(x == 0)
\n return a < b;
\n else
\n return a > b;
\n else
\n return x > y;
\n}
\nint main()
\n{
\n int n, i;
\n cin >> n;
\n int a[n];
\n for(i = 0; i < n; i++)
\n cin >> a[i];
\n sort(a, a+n, cmp);
\n for(i = 0; i < n; i++)
\n cout << a[i] << " ";
\n}