整数奇偶排序
整数奇偶排序
http://www.nowcoder.com/questionTerminal/bbbbf26601b6402c9abfa88de5833163
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int MAXSIZE = 10; int a[MAXSIZE]; int odd[MAXSIZE]; int even[MAXSIZE]; int main() { // int n; int p; int q; while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]) { p=-1;//这个p是下标,容易和个数混淆,如果不算好,就容易缺数少数,数量有时候用于i<n,排序arr+n, #1 q=-1; for(int i=0; i<10; ++i) { // scanf("%d",&a[i]); if(a[i]%2==1) { p++; odd[p]=a[i]; } else { q++; even[q]=a[i]; } } sort(odd,odd+p+1);//p+1 #2 sort(even,even+q+1); for(int i=p; i>-1; --i) { printf("%d ",odd[i]); } for(int i=0; i<q+1; ++i)//i<q+1 #3 { printf("%d ",even[i]); } } }