当前位置:网站首页>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;
}
边栏推荐
- Apache Doris 成为 Apache 顶级项目
- 两道面试小Demo
- Valentine's Day - VBS learning (sentences, love words)
- Privacy computing fat----- offline prediction
- How to suppress SiC MOSFET crosstalk?
- STL - inverter
- Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance
- Installation of containerd1.5.5
- [.Net6] GRP server and client development cases, as well as the access efficiency duel between the minimum API service, GRP service and traditional webapi service
- Applet: traverse the value of an array in the list, which is equivalent to for= "list" list An item in comment
猜你喜欢

RMAN backup message ora-19809 ora-19804

Guangzhou: new financial activities and new opportunities for enterprises

spark的资源调度和任务调度

JMeter -- interface test 2

Implementation of code scanning login

【云原生 | Kubernetes篇】深入了解Pod(六)

From knowledge to wisdom: how far will the knowledge map go?

How to solve the problem of port number occupation

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

Characteristics and prevention of electrical fire
随机推荐
rman备份报ORA-19809 ORA-19804
MySQL8.0 忘记 root 密码
Build the first neural network with pytoch and optimize it
使用transform:scale之后导致页面鼠标悬浮事件消失
Application of current limiting protector in preventing electrical fire in shopping malls
Almost union find (weighted union search)
State machine program framework
Goldbach`s Conjecture
new URL(“www.jjj.com“)
Rman Backup Report Ora - 19809 Ora - 19804
Decision table method for basic content learning of software testing (2)
Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
网上炒股开户安不安全?
Fire fighting work and measures in Higher Vocational Colleges
SQL 優化經曆:從 30248秒到 0.001秒的經曆
What are the advantages of a differential probe over a conventional probe
Data modeling based on wide table
Boundary value analysis method for learning basic content of software testing (2)
How do I open an account on my mobile phone? Is it safe to open an account online now?
【大案例】学成在线网站