牛客oj 习题3.1特殊排序&&习题3.2整数奇偶排序

两道送分题,不多说。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <climits>

using namespace std;

const int MAXN = 1005;
const int INF = INT_MAX;

int arr[MAXN];

int main(){
//	freopen("in.txt", "r", stdin);
	int N;
	while(~scanf("%d", &N)){
		for(int i = 0; i < N; i++){
			scanf("%d", &arr[i]);
		}
		if(N == 1){
			printf("%d\n", arr[0]);
			printf("-1\n");
			continue;
		}
		sort(arr, arr+N);
		printf("%d\n", arr[N-1]);
		bool flag = false;
		for(int i = 0; i < N-1; i++){
			if(flag) printf(" ");
			printf("%d", arr[i]);
			flag = true;
		}
		printf("\n");
	}
	return 0;
}
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <climits>

using namespace std;

const int MAXN = 11;
const int INF = INT_MAX;

bool cmp(int x, int y){
	return x > y;
}

int main(){
//	freopen("in.txt", "r", stdin);
	int N;
	int a[MAXN];
	while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]){
		int odd[MAXN], even[MAXN];
		int numodd = 0, numeven = 0;
		for(int i = 0; i < 10; i++){
			if(a[i]%2 == 1) odd[numodd++] = a[i];
			else even[numeven++] = a[i];
		}
		sort(odd, odd+numodd, cmp);
		sort(even, even+numeven);
		bool flag = false;
		for(int i = 0; i < numodd; i++){
			if(flag) printf(" ");
			printf("%d", odd[i]);
			flag = true;
		}
		for(int i = 0; i < numeven; i++){
			if(flag) printf(" ");
			printf("%d", even[i]);
			flag = true;
		}
		printf("\n");
	}
	return 0;
}
发布了411 篇原创文章 · 获赞 72 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/104381597