男孩vs女孩
1. 题目地址
https://www.acwing.com/problem/content/1522/
2. 题目解析
在读取字符串时,需要注意:
1. 如果字符串中含有空格,则无法使用cin来读取,因为cin遇到空格会自动停止。此时应该使用getline函数来进行读取。
2. 如果字符串中不含有空格,则直接使用cin读取即可。
这道题的字符串是不含有空格的,因此使用cin读取即可。
3. 题解
这道题从整体上看比较简单,这里不再赘述,直接看代码即可。做字符串处理这种类型的题,需要细心。
4. 代码
#include <iostream>
#include <cstdio>
using namespace std;
string girl_id,girl_name;
int girl_score;
string boy_id,boy_name;
int boy_score;
int n;
int main(){
scanf("%d",&n);
while(n--){
string id,name,sex;
int score;
cin >> name >> sex >> id;
scanf("%d",&score);
if(sex == "M"){
if(boy_id.empty() || score < boy_score){
boy_id = id;
boy_name = name;
boy_score = score;
}
}else{
if(girl_id.empty() || score > girl_score){
girl_id = id;
girl_name = name;
girl_score = score;
}
}
}
if(girl_id.empty()){
cout << "Absent" << endl;
}else{
cout << girl_name << " " << girl_id << endl;
}
if(boy_id.empty()){
cout << "Absent" << endl;
}else{
cout << boy_name << " " << boy_id << endl;
}
if(girl_id.empty() || boy_id.empty()){
cout << "NA" << endl;
}else{
cout << girl_score - boy_score << endl;
}
return 0;
}
作者:gao79138
链接:https://www.acwing.com/
来源:本博客中的截图、代码模板及题目地址均来自于Acwing。其余内容均为作者原创。
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具