POJ 3617 Best Cow Line 贪心+模拟

题目

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;

int N;
bool Com(string& str, int head, int tail) //比较逻辑,更方便的办法是用一个逆置后的字符串和原先的
{										  //字符串比较
    while (head <= tail) {
        if (str[head] < str[tail]) {
            return true;
        }
        else if (str[head] > str[tail]) {
            return false;
        }
        else {
            head++, tail--;
        }
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N;
    string str(N, '0');
    for (int i = 0; i < N; i++) {
        cin >> str[i];
    }
    string T;
    while (T.size() < N) {
        int len = str.size(), idx = 0;
        if (Com(str, 0, len - 1)) {
            T.insert(T.size(), 1, str[0]);
            str.erase(0, 1);
        }
        else {
            T.insert(T.size(), 1, str[len - 1]);
            str.erase(len - 1, 1);
        }
    }
    for (int i = 0; i < T.size(); i++) {
        cout << T[i];
        if ((i + 1) % 80 == 0) {
            cout << endl;
        }
    }
    return 0;
}

发布了36 篇原创文章 · 获赞 0 · 访问量 2065

猜你喜欢

转载自blog.csdn.net/weixin_43971049/article/details/104148998
今日推荐