结构体

· · 个人记录

定义

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

输入

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

输出

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

自定义比较函数

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输出;

#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;
}