快速提升代碼能力(7)sort(a,a+n);

從零起步看算法(第七天 4.12)

//q9 交叉排序

算是一道水題了。

1.花點時間看一下樣例,就可以理解題意了。

2.本題唯一知識點,用sort()實現,數組的從大到小排序。

很簡單的操作

https://blog.csdn.net/jimmy_lee1106/article/details/11746131

3.還要注意sort(a,a+n);兩個參數與下標不同。

#include<stdio.h>
#include<string>
#include<cstring>
#include<iostream> 
#include<assert.h>
#include<cmath>
#include<algorithm>
using namespace std; 
#define maxn 100000
//c++中從大到小排列 
bool cmp(int a,int b){
	return a>b;
}
void fun(int a[],int n,int l1,int r1,int l2,int r2){
	sort(a+l1-1,a+r1);
	sort(a+l2-1,a+r2,cmp);
	for(int i=0;i<n;i++){
		if(i!=0)
		{
			cout<<" ";
		}
		cout<<a[i];
	} 
	cout<<endl;
}
int main(){

	int N,l1,r1,l2,r2;
	cin>>N>>l1>>r1>>l2>>r2;
	int a[N+5];
	memset(a,0,sizeof(a));
	assert(l1<r1&&l2<r2);
	for(int i=0;i<N;i++){
		cin>>a[i];
	}
	fun(a,N,l1,r1,l2,r2);
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章