(map)7-8 宿舍谁最高? (25 分)

7-8 宿舍谁最高? (25 分)

学校选拔篮球队员,每间宿舍最多有4个人。现给出宿舍列表,请找出每个宿舍最高的同学。定义一个学生类Student,有身高height,体重weight等。

输入格式:

首先输入一个整型数n (1<=n<=1000000),表示n位同学。
紧跟着n行输入,每一行格式为:宿舍号,name,height,weight。
宿舍号的区间为[0,999999], name 由字母组成,长度小于16,height,weight为正整数。

输出格式:

按宿舍号从小到大排序,输出每间宿舍身高最高的同学信息。题目保证每间宿舍只有一位身高最高的同学。

输入样例:

7
000000 Tom 175 120
000001 Jack 180 130
000001 Hale 160 140
000000 Marry 160 120
000000 Jerry 165 110
000003 ETAF 183 145
000001 Mickey 170 115

输出样例:

000000 Tom 175 120
000001 Jack 180 130
000003 ETAF 183 145

感悟: 总的解题思路是对的,注意宿舍编号六位,字符串输入的话注意补齐,整型存储的话,输出的话注意控制格式。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=25;
typedef struct Node{
    char name[20];
    int h,w;
}student;
string id,t;

int main(){
    int n;
    cin>>n;
    getchar();
    set<string> s;
    map<string,student> mp;
    student tmp,p;
    for(int i=0;i<n;i++){
    	id="";
        cin>>t;
        for(int i=0;i<6-t.length();i++){
        	id+='0';
		}
		id+=t;
        cin>>tmp.name>>tmp.h>>tmp.w;
        if(s.find(id)==s.end()){
            mp[id]=tmp;
        }else{
            p=mp[id];
            if(p.h<tmp.h) mp[id]=tmp;
        }
         s.insert(id);
    }
  
   set<string> ::iterator it;
    for(it=s.begin();it!=s.end();it++){
        string t=*it;
        cout<<t<<" "<<mp[t].name<<" "<<mp[t].h<<" "<<mp[t].w<<endl;
       
    }
    return 0;
}

另一种写法:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=25;
typedef struct Node{
    char name[20];
    int h,w;
    Node(){
    	h=0;
    	w=0;
	}
}student;

int main(){
    int n,id;
    cin>>n;
    getchar();
    map<int,student> mp;
    student tmp;
    for(int i=0;i<n;i++){
        cin>>id;
        cin>>tmp.name>>tmp.h>>tmp.w;
        if(mp[id].h<tmp.h) mp[id]=tmp;
    }
  
  	map<int,student> ::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
    	int t=it->first;
    	printf("%06d",t);
      cout<<" "<<mp[t].name<<" "<<mp[t].h<<" "<<mp[t].w<<endl;
      //cout<<" "<<(it->second).name<<" "<<(it->second).h<<" "<<(it->second).w<<endl;
    }
    return 0;
}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值