2 solutions

  • 1
    @ 2024-8-23 16:15:09
    #include<bits/stdc++.h>
    using namespace std;
    struct Person {
        string sex;
        double height;
    };
    const int N=50;
    Person p[N];
    bool cmp(Person a,Person b)
    {
        if(a.sex=="male"&&b.sex=="female") return 1; //男生在左女生在右 
        if(a.sex=="male"&&b.sex=="male"&&a.height<b.height) return 1; //都是男生,升序 
        if(a.sex=="female"&&b.sex=="female"&&a.height>b.height) return 1; //都是女生,降序 
        return 0;
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>p[i].sex>>p[i].height;
        }
        sort(p,p+n,cmp);
        for(int i=0;i<n;i++)
        {
            cout<<fixed<<setprecision(2)<<p[i].height<<" "; 
        }
        return 0;
    }
    
    • 0
      @ 2026-9-19 16:22:49
      #include<bits/stdc++.h>
      using namespace std;
      struct Person {
      	string sex;
      	double height;
      };
      const int N=50;
      Person p[N];
      bool cmp(Person a,Person b)
      {
      	if(a.sex=="male"&&b.sex=="female") return 1; 
      	if(a.sex=="male"&&b.sex=="male"&&a.height<b.height) return 1;   
      	if(a.sex=="female"&&b.sex=="female"&&a.height>b.height) return 1;   
      	return 0;
      }
      int main()
      {
      	int n;
      	cin>>n;
      	for(int i=0;i<n;i++)
      	{
      		cin>>p[i].sex>>p[i].height;
      	}
      	sort(p,p+n,cmp);
      	for(int i=0;i<n;i++)
      	{
      		cout<<fixed<<setprecision(2)<<p[i].height<<" "; 
      	}
      	return 0;
      }
      • 1

      Information

      ID
      947
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      1
      Tags
      (None)
      # Submissions
      58
      Accepted
      28
      Uploaded By