概述:题目大体意思为古代的田忌赛马,现在要求将田忌赛马的过程用编程做出。
思路:整个步骤使用贪心算法,将两个人拥有的马排序之后,将田忌最强的马与国王最强的马比较,若是能胜利,那么胜利加,若是不能,就将田忌最弱的马与国王最强的马比赛,以此类推。
感谢:昨天晚上第一次提交,因为只是考虑了例子的输入,没有深入考虑整道题可能有的输入,题目出错,上午提交了几次,都是因为小错误或是超时失败,最后在同学帮助下成功。
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
bool cmp(int &a, int &b)
{
return a<b;
}
int main() {
//ifstream cin("in.txt");
int n;
while (cin >> n&&n)
{
int win = 0, lose = 0;
int a[1001], b[1001];
for (int i = 0;i<n;i++)
cin >> a[i];
for (int i = 0;i<n;i++)
cin >> b[i];
sort(a, a + n, cmp);
sort(b, b + n, cmp);
int t_slow = 0, t_fast = n - 1;
int k_slow = 0, k_fast = n - 1;
while (t_slow <= t_fast)
{
if (a[t_fast]>b[k_fast])
{
win++;
t_fast--;
k_fast--;
}
else if (a[t_slow]>b[k_slow])
{
t_slow++;
k_slow++;
win++;
}
else
{
if (a[t_slow]<b[k_fast])
lose--;
t_slow++;
k_fast--;
}
}
cout << (win + lose) * 200 << endl;
}
return 0;
}