Problems
![](https://i-blog.csdnimg.cn/blog_migrate/7dac2b9a01e4324855c3de4757928f9c.png)
这一场比赛是在算法考试前日晚上做的,做了一小时然后去吃饭了,姑且只有AB两题……
![](https://i-blog.csdnimg.cn/blog_migrate/fcd85338de204437c691d95ab2f3c1b9.png)
题意理解了之后这就是一个简单的代入计算问题啦~
double mp=max(3*a/10 , a-(a/250*c) );
double vp=max(3*b/10 , b-(b/250*d) );
Code:
#include <cmath>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
bool cmp(const int a, const int b)
{
return a > b;
}
int main()
{
int a=0,b=0,c=0,d=0;
cin>>a>>b>>c>>d;
double mp=max(3*a/10 , a-(a/250*c) );
double vp=max(3*b/10 , b-(b/250*d) );
if(mp>vp) cout<<"Misha";
else if(mp<vp) cout<<"Vasya";
else cout<<"Tie";
return 0;
}
我们可以意识到,中间的名字根本没有意义,当A变成B,B变成C的时候等价于A变成C,那么我们就直接记录当前的“原始-现名”关系即可,这里我使用了map,map<string,string>就可以实现 m[A]=B,然后当新的B变成C时更改为m[A]=C即可,这需要对STL中map的理解,当然用struct node也是可以的,题目中放宽要求可以in any order应该就是这个考虑吧,map可以自动把对应关系按照原始名的字典序排序,也是相当便利的事情。
Code:
#include <map>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef map<string, string> mss;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
bool cmp(const int a, const int b)
{
return a > b;
}
int main()
{
int n=0; cin>>n;
mss mp;
while(!mp.empty()) mp.clear();
for(int i=0;i<n;i++)
{
int flag=1;
string a,b; cin>>a>>b;
for (map<string,string>::iterator it=mp.begin(); it!=mp.end(); ++it)
{
if(it->second==a)
{
mp[it->first]=b;
flag=0;
break;
}
}
if(flag) mp[a]=b;
}
cout<<mp.size()<<endl;
for (map<string,string>::iterator it=mp.begin(); it!=mp.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}