#include <iostream>
#include <vector>
using namespace std;
void change(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void quickSort(int* a, int l, int u)
{
int i, m;
if (l >= u) return;
m = l;
for (i = l + 1; i <= u; i++)
if (a[i] > a[l])
change(a[++m], a[i]);
change(a[l], a[m]);
quickSort(a, l, m - 1);
quickSort(a, m + 1, u);
}
int main(void)
{
vector<int> result;
vector<int> ra;
vector<int> rb;
int num;
int* a;
int* b;
cout<<"请输入马匹数量:";
while (cin>>num)//输入马匹数量
{
if (num == 0)
break;
a = new int[num];
b = new int[num];
cout<<"输入田忌的马速度:";
for (int i = 0; i < num; i++)
cin>>a[i];
cout<<"输入齐王的马速度:";
for (int i = 0; i < num; i++)
cin>>b[i];
cout<<"如需修改请重输马数量,否则输入0结束输入:";
quickSort(a, 0, num-1);
quickSort(b, 0, num-1);
int win = 0;
int fail = 0;
int draw = 0;
int ib = 0, jb = 0;
int ie = num - 1, je = num - 1;
while ( ib <= ie )
{
if (a[ie] > b[je])
{
win++;
ie--;
je--;
}else if (a[ie] < b[je])
{
fail++;
ie--;
jb++;
}else
{
if (a[ib] > b[jb])
{
win++;
ib++;
jb++;
}else
{
if (a[ie] < b[jb])
fail++;
ie--;
jb++;
}
}
}
result.push_back(200*(win - fail));
}
for (size_t i = 0; i != result.size(); i++)
{
cout<<"最多田忌可以赢 ";
cout<<result[i]<<" 两"<<endl;
}
return 0;
}
c/C++ 田忌赛马(贪心算法)
原文作者:贪心算法
原文地址: https://blog.csdn.net/hubaoquanu/article/details/51606756
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/hubaoquanu/article/details/51606756
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。