HDU水题(5)

1048 The Hardest Problem Ever

注意gets读取一行时把’\n’转换为’\0’,puts输出一个字符串时把’\0’转换为’\n’。

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

void decode(char& code)
{
    if (code>'Z' || code < 'A')
        return;

    code -= 5;
    if (code < 'A')
        code += 26;
}

void decodeStr(char* str)
{
    int len = strlen(str);
    for (int i = 0; i < len; i++)
    {
        decode(str[i]);
    }
}

int main()
{
    while (1)
    {
        char str[10000];
        gets(str);
        if (!strcmp(str, "ENDOFINPUT"))
            break;

        gets(str);
        decodeStr(str);
        puts(str);
        gets(str);
    }
}

1049 Climbing Worm

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

int main()
{
    int n, u, d;
    while (scanf("%d%d%d", &n, &u, &d) != EOF)
    {
        if (!n)
            break;

        if((n - u) % (u - d))
            printf("%d\n", ((n - u) / (u - d) + 1) * 2 + 1);
        else
            printf("%d\n", ((n - u) / (u - d)) * 2 + 1);
    }
}

1052 Tian Ji – The Horse Racing

田忌赛马,策略如下:(贪婪)
1.比较田最快的和齐最快的,田快直接p掉,齐快用田最慢的换掉,平局则进入2
2.比较田最慢的和齐最慢的,田快直接p掉,齐快用田最慢的换掉齐最快的,平局则进入3
3.最快最慢都平局,用田最慢的换掉齐最快的

显然以上每一步都是最优解

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>

int sumWin = 0, sumTie = 0;

bool cmp(int a, int b)
{
    return a > b;
}

void race(std::vector<int>::iterator tBegin, std::vector<int>::iterator tEnd, std::vector<int>::iterator kBegin, std::vector<int>::iterator kEnd)
{
    if (tBegin == tEnd)
    {
        if (*tBegin == *kBegin)
            sumTie++;
        if (*tBegin > *kBegin)
            sumWin++;
        return;
    }

    if (*tBegin > *kBegin)
    {
        sumWin++;
        race(tBegin + 1, tEnd, kBegin + 1, kEnd);
        return;
    }
    if (*tBegin < *kBegin)
    {
        race(tBegin , tEnd - 1, kBegin + 1, kEnd);
        return;
    }
    if (*tBegin == *kBegin)
    {
        if (*tEnd > *kEnd)
        {
            sumWin++;
            race(tBegin , tEnd - 1, kBegin, kEnd - 1);
            return;
        }
        if (*tEnd < *kEnd)
        {
            race(tBegin, tEnd - 1, kBegin + 1, kEnd);
            return;
        }
        if (*tEnd == *kEnd)
        {
            if (*tEnd == *kBegin)
                sumTie++;
            race(tBegin, tEnd - 1, kBegin + 1, kEnd);
            return;
        }

    }
}

int main()
{
    int num;
    while (scanf("%d", &num) != EOF)
    {

        if (!num)
            break;

        int spd;
        std::vector<int> tian, king;
        for (int i = 0; i < num; i++)
        {
            scanf("%d", &spd);
            tian.push_back(spd);
        }
        for (int i = 0; i < num; i++)
        {
            scanf("%d", &spd);
            king.push_back(spd);
        }
        sort(tian.begin(), tian.end(), cmp);
        sort(king.begin(), king.end(), cmp);
        sumWin = 0; sumTie = 0;
        race(tian.begin(), tian.end() - 1, king.begin(), king.end() - 1);
        int sumLose = num - sumWin - sumTie;
        int earn = (sumWin - sumLose) * 200;
        printf("%d\n", earn);
    }
}

1062 Text Reverse

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>


void reverse(char* str, int len)
{
    char* begin = str, * end = str + len - 1;
    while (begin < end)
    {
        char temp = *begin;
        *begin = *end;
        *end = temp;
        begin++;
        end--;
    } 
}

void allReverse(char* str)
{
    char* begin = str, * end = str;
    while (*end != '\0')
    {
        while (*begin == ' ')
            begin++;

        end = begin;
        while (*end != ' ' && *end != '\0')
                end++;

        reverse(begin, end - begin);
        begin = end;
    }
}

int main()
{
    int num;
    scanf("%d", &num);
    getchar();
    while (num--)
    {
        char str[10000], c;
        gets(str);
        allReverse(str);
        puts(str);
    }
}

1064 Financial Management

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>




int main()
{
    double num,total = 0;
    for (int i = 0; i < 12; i++)
    {
        scanf("%lf", &num);
        total += num;
    }
    total /= 12;
    printf("$%.2lf\n", total);

}

1070 Milk

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>




int main()
{
    int numCase;
    scanf("%d", &numCase);
    for (int i = 0; i < numCase; i++)
    {
        int numBrand;
        scanf("%d", &numBrand);
        float cheapPrice = 2e9, cheapVolume = 0;
        char cheapBrand[105];
        for (int i = 0; i < numBrand; i++)
        {
            char brand[105];
            int price, volume;
            scanf("%s%d%d", brand, &price, &volume);
            int day = volume / 200 > 5 ? 5 : volume / 200;
            if (!day)
                continue;

            float avrPrice = (float)price / day;
            if (avrPrice < cheapPrice)
            {
                cheapPrice = avrPrice;
                cheapVolume = volume;
                strcpy(cheapBrand, brand);
            }
            else if (avrPrice == cheapPrice)
            {
                if (volume > cheapVolume)
                {
                    cheapPrice = avrPrice;
                    cheapVolume = volume;
                    strcpy(cheapBrand, brand);
                }
            }
        }
        printf("%s\n", cheapBrand);
    }

}

1073 Online Judge

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;

bool check(char p)
{
    if (p == ' ')
        return false;
    else if (p == '\t')
        return false;
    else if (p == '\n')
        return false;
    else
        return true;
}

int main()
{
    int numCase;
    scanf("%d", &numCase); getchar();
    for (int i = 0; i < numCase; i++)
    {
        char str[5005];
        string context1,str1 = "";
        string context2,str2 = "";

        gets(str);
        if (strcmp(str, "START"))
            break;

        while (1)
        {
            gets(str);
            if (!strcmp(str, "END"))
                break;

            context1.append("\n");
            context1.append(str);
        }
        strcpy(str,context1.c_str());
        for (int i = 0; i < strlen(str); i++)
        {
            if (check(*(str + i)))
            {
                str1 = str1 + *(str + i);
            }
        }

        gets(str);
        if (strcmp(str, "START"))
            break;

        while (1)
        {
            gets(str);
            if (!strcmp(str, "END"))
                break;

            context2.append("\n");
            context2.append(str);
        }
        strcpy(str, context2.c_str());
        for (int i = 0; i < strlen(str); i++)
        {
            if (check(*(str + i)))
            {
                str2 = str2 + *(str + i);
            }
        }

        if (!strcmp(context1.c_str(), context2.c_str()))
            printf("Accepted\n");
        else if (!strcmp(str1.c_str(), str2.c_str()))
            printf("Presentation Error\n");
        else
            printf("Wrong Answer\n");


    }

}
发布了17 篇原创文章 · 获赞 0 · 访问量 568

猜你喜欢

转载自blog.csdn.net/kybxdkybxd/article/details/103172143