当前位置:网站首页>1181:整数奇偶排序
1181:整数奇偶排序
2022-06-28 09:06:00 【暴揍键盘的程序猿】
1181:整数奇偶排序
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 21478 通过数: 13938
【题目描述】
给定10个整数的序列,要求对其重新排序。排序要求:
1.奇数在前,偶数在后;
2.奇数按从大到小排序;
3.偶数按从小到大排序。
【输入】
输入一行,包含10个整数,彼此以一个空格分开,每个整数的范围是大于等于0,小于等于30000。
【输出】
按照要求排序后输出一行,包含排序后的10个整数,数与数之间以一个空格分开。
【输入样例】
4 7 3 13 11 12 0 47 34 98
【输出样例】
47 13 11 7 3 0 4 12 34 98
【思路】
把这10个数分成奇数和偶数排序即可。
【一堆30分代码】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
#define for(i,n,m) for(int i=n;i<=m;i++)
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(i,1,10)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n,greater<int>()),sort(c+1,c+1+n);
for(i,1,n)cout<<b[i]<<" ";
for(i,1,m)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=15;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int x,b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
x=fread();
if(x&1)b[++n]=x;
else c[++m]=x;
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

【干脆复制了一篇博客的AC代码】
#include <bits/stdc++.h>
using namespace std;
bool cmpUp(int a, int b)//升序
{
return a < b;
}
bool cmpDown(int a, int b)//降序
{
return a > b;
}
int main()
{
int a, odd[15], even[15], oi = 0, ei = 0;//oi:odd中元素个数 ei:even中元素个数
for(int i = 1; i <= 10; ++i)
{
cin >> a;
if(a%2 == 0)
even[++ei] = a;//填充数组
else
odd[++oi] = a;
}
sort(odd+1, odd+1+oi, cmpDown);//奇数降序排序
sort(even+1, even+1+ei, cmpUp);//偶数升序排序
for(int i = 1; i <= oi; ++i)
cout << odd[i] << ' ';
for(int i = 1; i <= ei; ++i)
cout << even[i] << ' ';
return 0;
}

【又调试出一堆30分代码】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<b[i]<<' ';
for(int i=1;i<=m;i++)cout<<c[i]<<' ';
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=15;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n=0,m=0;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
bool cmp(int n,int m)
{
return n>m;
}
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n,cmp),sort(c+1,c+1+n);
for(int i=1;i<=n;i++)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>=1;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]%2==0)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<c[i]<<" ";
for(int i=1;i<=m;i++)cout<<b[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]%2==0)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+n);
for(int i=n;i>0;i--)cout<<c[i]<<" ";
for(int i=1;i<=m;i++)cout<<b[i]<<" ";
return 0;
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]%2==0)b[++n]=a[i];
else c[++m]=a[i];
}
sort(b+1,b+1+n),sort(c+1,c+1+m);
for(int i=n;i>0;i--)cout<<c[i]<<" ";
for(int i=1;i<=m;i++)cout<<b[i]<<" ";
return 0;
}

【AC代码】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e5+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
int a[N],b[N],c[N],n,m;
signed main()
{
for(int i=1;i<=10;i++)
{
a[i]=fread();
if(a[i]&1)b[++n]=a[i];//奇数
else c[++m]=a[i];//偶数
}
sort(b+1,b+1+n),sort(c+1,c+1+m/*之前手滑把m写成了n才导致那么多WA*/);//分开排序
for(int i=n;i>0;i--)cout<<b[i]<<" ";
for(int i=1;i<=m;i++)cout<<c[i]<<" ";
return 0;
}

边栏推荐
- Zhejiang energy online monitoring and management system
- 如何抑制SiC MOSFET Crosstalk(串擾)?
- Key points of building fire protection design
- How to suppress SiC MOSFET crosstalk?
- Implementation of single sign on
- 買賣股票費用計算
- containerd1.5.5的安装
- Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
- rman備份報ORA-19809 ORA-19804
- Trailing Zeroes (II)
猜你喜欢
Apiccloud, together with 360 Tianyu, helps enterprises keep the "first pass" of APP security
从知识到智慧:知识图谱还要走多远?
The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution
STL -- binder
Deployment of MySQL database in Linux Environment
Test cases for learning the basic content of software testing (II)
Protection range and optimization of motor protector for hoist equipment
Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
rman备份报ORA-19809 ORA-19804
Implementation of code scanning login
随机推荐
Operating principle of Rogowski coil
Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
Which is a better ERP management system for electronic component sales?
Integer partition
Understanding the IO model
我想网上注册股票开户,如何操作?在线开户安全么?
DEJA_VU3D - Cesium功能集 之 052-模拟卫星轨道(高空)效果
[untitled]
Illustration of MySQL binlog, redo log and undo log
SQL 优化经历:从 30248秒到 0.001秒的经历
Is it safe to open an account for online stock speculation?
Common faults and solutions of Substation
Mysql8.0 forgot the root password
Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance
MATLAB小技巧(20)矩阵分析--主成分回归
Goldbach`s Conjecture
買賣股票費用計算
买卖股票费用计算
STL - inverter
中金财富开户安全吗?怎么收费?