A30608.【结构体】【排序】合影效果
2024-12-28 15:47:28
发布于:江苏
57阅读
0回复
0点赞
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct student{
string s;
float h;
bool male;
};
bool cmp(student a,student b){
if(a.male!=b.male) return a.male>b.male;
if(a.male&&b.male) return a.h<b.h;
return a.h>b.h;
}
int main(){
int n;
cin>>n;
student p[40];
for(int i=0;i<n;i++){
cin>>p[i].s>>p[i].h;
p[i].male=(p[i].s=="male");
}
sort(p,p+n,cmp);
for(int i=0;i<n;i++)
printf("%.2f ",p[i].h);
return 0;
}
全部评论 1
#include<bits/stdc++.h>
using namespace std;
struct person{
string sex;
float height;
}a[50];
bool cmp(person a,person b){
if(a.sex!=b.sex){
if(a.sex=="male"){
return true;
}else{
return false;
}
}else{
if(a.sex=="male"){
return a.height < b.height;
}else{
return a.height > b.height;
}
}
}
int main(){
int n;
cin>>n;
for(int i = 0;i<n;i++){
cin>>a[i].sex>>a[i].height; //输入
}sort(a,a+n,cmp); for(int i = 0;i<n;i++){ printf("%.2lf ",a[i].height); } return 0;
}
2025-01-02 来自 江苏
0
有帮助,赞一个