PTA——宿舍谁最高7-1 宿舍谁最高? (20 分)

学校选拔篮球队员,每间宿舍最多有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

wa :

#include<iostream>
#include<string>
using namespace std;
class student
{public:float fight;float weight;string num;string name;public:void input();void output();
};
void student::input()
{cin>>num>>name>>fight>>weight;getchar();
}
void student::output()
{cout<<num<<" "<<name<<" "<<fight<<" "<<weight<<endl ;
}
void sort(student *c,int n)
{student temp;for(int i=0;i<n-1;++i){for(int j=i;j<n-1-i;++j){if(c[j].num>c[j+1].num){temp=c[j];c[j]=c[j+1];c[j+1]=temp;}}}
}
student sorts(student *c,int z,int n)
{student temp=c[z];for(int i=z+1;i<n+z;++i){if(c[i].fight>temp.fight){temp=c[i];}}return temp;
}
int main()
{int n,i;student *std=NULL;cin>>n;getchar();std = new student[n+1];//cout<<"hello"<<endl;for(i=0;i<n;++i){std[i].input();}std[i].name ="0000";std[i].num ="-9999";std[i].fight=-99;std[i].weight =-99;if(n>1){sort(std,n);student temp;int  flag=1;for(i=0;i<n;++i){if(std[i].num ==std[i+1].num ){flag++;}else{temp=sorts(std,i-flag+1,flag);temp.output();flag=1;}}}else{std[0].output();}return 0;
}

一开始采用string来存放宿舍号其实是不对的,比如:宿舍号为"1"与“23”的,如果输入“1”,“023”,则 “1”>“23”

AC:

#include<iostream>
#include<string>
#include<stdio.h>
#include<iomanip>
using namespace std;
class student
{public:float fight;float weight;int  num;string name;public:void input();void output();
};
void student::input()
{cin>>num>>name>>fight>>weight;getchar();
}
void student::output()
{cout<<setw(6)<<setfill('0')<<num;cout<<" "<<name<<" "<<fight<<" "<<weight<<endl ;
}
void sort(student *c,int n)
{student temp;for(int i=0;i<n-1;++i){for(int j=i;j<n-1-i;++j){if(c[j].num>c[j+1].num){temp=c[j];c[j]=c[j+1];c[j+1]=temp;}}}
}
student sorts(student *c,int z,int n)
{student temp=c[z];for(int i=z+1;i<n+z;++i){if(c[i].fight>temp.fight){temp=c[i];}}return temp;
}
int main()
{int n,i;student *std=NULL;cin>>n;getchar();std = new student[n+1];//cout<<"hello"<<endl;for(i=0;i<n;++i){std[i].input();}std[i].name ="0000";std[i].num =-99999;std[i].fight=-99;std[i].weight =-99;if(n>1){sort(std,n);student temp;int  flag=1;for(i=0;i<n;++i){if(std[i].num ==std[i+1].num ){flag++;}else{temp=sorts(std,i-flag+1,flag);temp.output();flag=1;}}}else{std[0].output();}return 0;
}

another:使用map进行存放,可以直接省略掉sort()函数(在我们插入<key, value>键值对时,map就会自动按照key的大小顺序进行存储)

PTA——宿舍谁最高

PTA:宿舍谁最高?相关推荐

  1. zuiqingchun4

    第一百六十八章 给我介绍对象? 不过仔细想想,最可笑的就是自己了,高帅现在有了新的对象了,是我们学校一个女的,陈少秋现在也正跟林一妹好着呢,人家可能根本就不会坐着想这些无聊的问题,只有我自己在这角落里 ...

  2. PTA 7-1 宿舍谁最高?(20分)

    这是一个菜鸡的挣扎过程!!不要嫌弃本辣鸡55555 输入格式: 首先输入一个整型数n (1<=n<=1000000),表示n位同学. 紧跟着n行输入,每一行格式为:宿舍号,name,hei ...

  3. C语言 之 PTA乙级错误集锦

    1,很大很大的数输入,并各位加和  PTA-1001 #include <stdio.h> #include <math.h> int main(){int sum=0,cou ...

  4. Java项目:宿舍寝室维修上报管理系统(java+SpringBoot+FreeMarker+Mysql)

    源码获取:博客首页 "资源" 里下载! 管理员:校园管理(楼栋管理.宿舍管理).师生管理(学生管理.辅导员管理).维修管理(维修工管理.维修进度管理).阅览室管理(座位生成等).学 ...

  5. Java项目:宿舍管理系统(java+jsp+SSM+Spring+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能:包括学生管理,班级管理,宿舍管理,人员信息维 护.维修登记,卫生管理,访客管理等等. 二.项目运行 环境配置: Jdk1 ...

  6. Java学生宿舍管理系统,即将毕业的兄弟有福了!

    点击关注公众号,Java干货及时送达 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接: https://blog.csdn.net/ ...

  7. 博士生宿舍条件太好,本科生因疫情暂住惊到结巴!网友:不然咱读个博?

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文系募格课堂整合,参考来源:闪电新闻.各校官微.微博.知乎 总听说 ...

  8. “偷懒”上热搜!南京大三学生自制宿舍关灯神器火了,网友:希望量产

    金磊 发自 凹非寺 量子位 报道 | 公众号 QbitAI 宿舍关灯这件事情,最近比较火. 起因是一位南京信息工程大学大三学生,花了7.8天时间,自制了一款宿舍关灯神器. 效果是这样的. 点击一下手机 ...

  9. 用脚写字考上985!无臂硕士开学报到,宿舍设计太细节了……

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 6岁不幸双臂截肢,用脚写字考上研究生,无臂少年彭超的故事令人动容. ...

最新文章

  1. 只有程序员才能看懂的30张图,绝配!
  2. python 调用linux命令-Python 执行Linux系统命令的N种方法
  3. docker的学习总结
  4. C++(Goto使用实例)
  5. 使用jmeter对ActiveMQ集群性能方案进行评估--转载
  6. 【收藏】SpringBlade开发手册地址
  7. flutter 日历_Flutter:一个更贴近真实项目的练习
  8. 一日一技:在Ocelot网关中统一配置Swagger
  9. [剑指offer]面试题34:丑数
  10. 【算法分析与设计】汉诺塔问题
  11. linux四剑客-grep/find/sed/awk/详解-技术流ken
  12. Hibernate基于Maven入门实例,与MyBatis比对
  13. arm-linux启动,linux启动流程arm
  14. 6.4 First Missing Positive --- 图解
  15. vue实现全国省市下拉联动菜单,适用于收货地址
  16. 数字图像处理(MATLAB版
  17. 13个非常有用的Python代码片段
  18. 翡翠手链的形成来源于古人对于图腾的崇拜
  19. 在2018年使用xp是怎样的体验
  20. 区块链技术能否给跨境贸易带来新的机遇?

热门文章

  1. 智慧路灯杆建设完成后的运营内容及运营方式有哪些?
  2. Mitaka正式发布 盘点OpenStack中国贡献
  3. 新网银行金融科技挑战赛 AI算法赛道 亚军方案
  4. uniapp 权限树模块- ly-tree组件
  5. 硬件描述语言VHDL
  6. 硬件描述语言实验五:四位加法器实验
  7. PB UpperBound()方法
  8. 利用OpenXml生成Word2007文档
  9. stm32H7 擦除读写SPI NOR FLASH时间测试
  10. react条件渲染(类似Vue中的v-show或v-if)