当前位置:网站首页>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;
}
边栏推荐
- [untitled]
- 使用transform:scale之后导致页面鼠标悬浮事件消失
- 学习阿里如何进行数据指标体系的治理
- Where is CentOS mysql5.5 configuration file
- Three body attack (three-dimensional split plus two points)
- Operating principle of Rogowski coil
- Which is a better ERP management system for electronic component sales?
- 买卖股票费用计算
- Power data
- 华泰证券网上开户安全吗 办理流程是什么
猜你喜欢

Discussion on the improvement and application of the prepayment system in the management of electricity charge and price

How to solve the problem of port number occupation

High rise building fire prevention

Application of current limiting protector in preventing electrical fire in shopping malls

APICloud携手三六零天御,助力企业守好App安全“第一关”

How to suppress SiC MOSFET crosstalk?

Rman Backup Report Ora - 19809 Ora - 19804

Characteristics and prevention of electrical fire

STL -- binder

Valentine's Day - VBS learning (sentences, love words)
随机推荐
Copy & Deepcopy
Privacy computing fat----- offline prediction
个人究竟如何开户炒股?在线开户安全么?
Quickly understand JVM structure and working principle
Rman Backup Report Ora - 19809 Ora - 19804
Android studio interview preparation
状态机程序框架
[big case] Xuecheng online website
图解MySQL的binlog、redo log和undo log
为什么SELECT * 会导致查询效率低?
Why does select * lead to low query efficiency?
如何抑制SiC MOSFET Crosstalk(串擾)?
rman备份报ORA-19809 ORA-19804
Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
Use of Jasper soft studio report tool and solution of thorny problems
Using transform:scale causes the page mouse hover event to disappear
Common faults and solutions of Substation
[untitled]
How to solve the problem of high concurrency and seckill
用Pytorch搭建第一個神經網絡且進行優化