贪心Hdu1052田忌赛马

两个版本我都写了不知道为什么一直wa=========啊
唉真是跟网上的代码一行一行都对过了,算了吃饭去了,难受
希望我可以早日明白到底是怎么回事
另外这位兄弟写的不错
https://www.cnblogs.com/DOLFAMINGO/p/7538779.html

#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    vector tian;
    vector qi;
    int h;
    int ans;
    int flag = 0;
    while (cin >> n&&n) {
        if (flag)
            cout << endl;
        flag = 1;
        ans = 0;
        for (int i = 1; i <= n; ++i) {
            cin >> h;
            tian.push_back(h);
        }
        for (int i = 1; i <= n; ++i) {
            cin >> h;
            qi.push_back(h);
        }
        sort(tian.rbegin(), tian.rend());//降序
        sort(qi.rbegin(), qi.rend());
        int l1 = 0, l2 = 0, r1 = n - 1, r2 = n - 1;//田和齐当前最好与最差的马
        while (l1 <= r1) {//注意这个循环终止条件
            if (tian[r1] > qi[r2]) {
                ans += 200;
                --r1; --r2;
            }
            else if (tian[r1] < qi[r2]) {
                ans -= 200;
                --r1; ++l2;
            }
            else {
                if (tian[l1] > qi[l2]) {
                    ans += 200;
                    ++l1; ++l2;
                }
                else if (tian[l1] < qi[l2]) {
                    ans -= 200;
                    --r1; ++l2;
                }
                else {
                    if (tian[r1] < qi[l2]) ans -= 200;
                    --r1; ++l2;
                }
            }

        }
        cout << ans;
    }
    system("pause");
    return 0;
}
#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    vector tian;
    vector qi;
    int h;
    int ans;
    int flag = 0;
    while (cin >> n&&n) {
        if (flag)
            cout << endl;
        flag = 1;
        ans = 0;
        for (int i = 1; i <= n; ++i) {
            cin >> h;
            tian.push_back(h);
        }
        for (int i = 1; i <= n; ++i) {
            cin >> h;
            qi.push_back(h);
        }
        sort(tian.rbegin(), tian.rend());//降序
        sort(qi.rbegin(), qi.rend());
        int l1 = 0, l2 = 0, r1 = n - 1, r2 = n - 1;//田和齐当前最好与最差的马
        while (l1 <= r1) {//注意这个循环终止条件
            if (tian[l1] qi[l2]) {
                 //田最好与齐最好vs赢
                ans += 200;
                l1++;
                l2++;
            }
            else if (tian[l1] == qi[l2]) {
                if (tian[r1] > qi[r2]) {
                    //田最差与齐最差vs赢
                    ans += 200;
                    --r1;
                    --r2;
                }
                else {
                    //田最差与齐最好vs
                    //注意这里有一种田最差与齐最好相等的可能所以和最上面
                    //田最好不如齐最好是不一样的
                    if (tian[r1] < qi[l2]) ans -= 200;
                    --r1;
                    ++l2;
                }
            }

        }
        cout << ans;
    }
    system("pause");
    return 0;
}

你可能感兴趣的:(贪心Hdu1052田忌赛马)