跳到正文

个人记录

结构体

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

定义

C++
struct node{
    int h;
    double f;
    //啥类型都能定义
}a[1000];

输入

C++
cin>>a[i].h>>a[i].f;

输出

C++
cout<<a[i].h<<a[i].f;

自定义比较函数

C++
bool cmp(node a, node b){
    return a.h<b.h
}
    sort(a+1,a+n+1,cmp);

错题纠正

不会写stuct P5412 [YNOI2019] 排队 思路错误

要用if判断判断一下,他是1还是0?如果是0就输出,然后再换行在判断,如果是1输出;

C++
#include<bits/stdc++.h>
using namespace std;
struct node{//将身高性别绑定
	double h;
	short sex;
}a[2010];
bool cmp(node a,node b){//从小到大排序
	if(a.h<b.h)return 1;
	else return 0;
}

int main(){
	int t,n;
	cin>>t;
	while(t--){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i].sex;//输入
	for(int i=1;i<=n;i++)cin>>a[i].h;

	sort(a+1,a+n+1,cmp);//排序
	for(int i=1;i<=n;i++){//判断加输出
		if(a[i].sex==0){
			cout<<a[i].h<<" ";
		}
	}
	cout<<endl;
		for(int i=1;i<=n;i++){
		if(a[i].sex==1){
			cout<<a[i].h<<" ";
		}
	}
}
return 0;
}
原文