跳到正文

题解

题解:B4361 [GESP202506 四级] 排序

NZK2013约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年7月5日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
获赞
1
题目
B4361

版本

  1. v1当前

T2 排序:

题目大意:

题目大意是给一群人数为n的黑皮体育生排序,按从高到矮排序,若身高相同就按体重从重到轻排(n<=3000,a[i]<=10^9)。

思路:

建一个类型为student的结构体数组,先排身高,再排相同身高下的体重,注意冒泡的数据交换。

大致框架:

C++
struct student{
	int h,w;
}a[3005];
int n,ans;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].h>>a[i].w;
	for(int i=n;i>1;i--){
		for(int j=1;j<i;j++){
			//......
		}
	}
	//......
	return 0;
}

你们最爱的:

C++
#include<bits/stdc++.h>
using namespace std;
int n,ans=0;
struct student{
	int h,w;
}a[3005];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].h>>a[i].w;
	for(int i=n;i>1;i--){
		for(int j=2;j<=i;j++){
			if(a[j-1].h<a[j].h){
				int tmp,tmpp;
				ans++;
				tmp=a[j-1].h;
				a[j-1].h=a[j].h;
				a[j].h=tmp;
				tmpp=a[j-1].w;
				a[j-1].w=a[j].w;
				a[j].w=tmpp;
			}
		}
	}
	for(int i=n;i>1;i--){
		for(int j=2;j<=i;j++){
			if(a[j-1].h==a[j].h&&a[j-1].w<a[j].w){
				int tmp,tmpp;
				ans++;
				tmp=a[j-1].h;
				a[j-1].h=a[j].h;
				a[j].h=tmp;
				tmpp=a[j-1].w;
				a[j-1].w=a[j].w;
				a[j].w=tmpp;
			}
		}
	}
	cout<<ans;
	return 0;
}

友链:

#1 GESP202506游记

#2 T1 画布裁剪 题解

原文