POJ 3617 Best Cow Line

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3617 Best Cow Line相关的知识,希望对你有一定的参考价值。

简单贪心。注意输出格式,每到80个字符就换一行。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn = 2000 + 10;
int a[maxn], ans[maxn];
int n;

void read()
{
    for (int i = 1; i <= n; i++)
    {
        char s[5]; scanf("%s", s);
        a[i] = s[0] - A + 1;
    }
}

void work()
{
    int first = 1, last = n;
    for (int i = 1; i <= n; i++)
    {
        int tmp = last - first + 1;
        bool f = 0;
        for (int j = 0; j<tmp / 2; j++)
        {
            if (a[first + j]>a[last - j])
            {
                ans[i] = a[last], last--, f = 1;
                break;
            }
            else if (a[first + j]<a[last - j])
            {
                ans[i] = a[first], first++, f = 1;
                break;
            }
        }
        if (f == 0) ans[i] = a[first], first++;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        printf("%c", ans[i] - 1 + A); cnt++;
        if (cnt == 80)
        {
            printf("\n");
            cnt = 0;
        }
    }
    printf("\n");
}

int main()
{
    while (~scanf("%d", &n))
    {
        read();
        work();
    }
    return 0;
}

 

以上是关于POJ 3617 Best Cow Line的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3617 Best Cow Line (贪心)

Best Cow Line---POJ 3617(贪心)

POJ 3617——Best Cow Line

poj 3617 Best Cow Line

POJ 3617 Best Cow Line

poj3617 Best Cow Line