解题思路
仍是模拟,该题是要求你分解问题,讲难的问题分过程逐步化简直到结束,例如本题
第一要对输入的信息组统一化红黄牌,即将获得两张黄牌的player在后一次获得黄牌时改成红牌(为什么是后一次获得黄牌呢?因为输出要求得到获得红牌的时间,获得两次黄牌的player在后一次获得黄牌时获得红牌)
第二我们要求对信息排列,标准包括时间、主客、编号,相信学到结构体这个地步的同学一定晓得这个问题怎么解决,(直接利用#algorithm sort(T a,T b,cmp)....具体可参考经典的生日排序问题)
解题代码
#include<bits/stdc++.h>
using namespace std;
struct player
{
int time;
string team;
int id;
char color;
};//结构体的设置不用多想,直接定义输入变量即可,需要其他变量编写过程中可以再加
bool cmp(player a,player b)
{
if(a.time==b.time)
{
if(a.team==b.team)
{
return a.id>b.id;
}
return a.team=="a"?0:1;
}
return a.time<b.time;
}//sort判据 time>team>id
int main()
{
string str1,str2;cin>>str1>>str2;
int n;cin>>n;
player st[n],en[n];//不要吝啬你的数组,OK?
for(int i=0;i<n;i++) cin>>st[i].time>>st[i].team>>st[i].id>>st[i].color;
for(int i=0;i<n;i++)
{
if(st[i].color=='y')
{
for(int j=i+1;j<n;j++)
{
if(st[j].color=='y'&&st[j].id==st[i].id&&st[j].team==st[i].team)
{
st[j].color='r';
}
}
}
}
int sum=0;//提炼红牌player
en[sum].time=0;//忘了有啥用了,可能就是一句废话?
for(int i=0;i<n;i++)
{
if(st[i].color=='r')
{
sum++;
en[sum].team=st[i].team;
en[sum].id=st[i].id;
en[sum].time=st[i].time;
}
}
if(!sum)
{
cout<<"No Red Card";
return 0;
}//如果sum不为0
sort(en,en+sum+1,cmp);
for(int i=1;i<sum+1;i++)
{
cout<<(en[i].team=="h"?str1:str2)<<' '<<en[i].id<<' '<<en[i].time<<endl;
}
}
问题分解!!!!
,