HDU水题(4)

1031 Design T-Shirt

建立一个map来存,然后对value排序并查找

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

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

int main()
{
    int people, design, choose;
    while (scanf("%d%d%d", &people, &design, &choose) != EOF)
    {
        std::map<int, double> total;
        for (int i = 0; i < design; i++)
        {
            total[i] = 0;
        }

        std::vector<double> prefer(design, 0);

        for (int i = 0; i <people; i++)
        {
            for (int j = 0; j < design; j++)
            {
                scanf("%lf", &prefer[j]);
                total[j] += prefer[j];
            }
        }

        for (int i = 0; i < design; i++)
        {
            prefer[i] = total[i];
        }

        std::sort(prefer.begin(), prefer.end(), cmp);
        std::vector<int> indice(choose, 0);

        std::map<int, double>::iterator it = total.begin();
        while(it != total.end())
        {
            if (it->second == prefer[0])
            {
                indice[0] = it->first;
                it++;
                break;
            }
            it++;
        }

        for (int i = 1; i < choose; i++)
        {
            if (prefer[i] != prefer[i - 1]) 
            {
                for (it = total.begin(); it != total.end(); it++)
                {
                    if (it->second == prefer[i])
                    {
                        indice[i] = it->first;
                        it++;
                        break;
                    }
                }
            }
            else
            {
                while (it != total.end())
                {
                    if (it->second == prefer[i])
                    {
                        indice[i] = it->first;
                        it++;
                        break;
                    }
                    it++;
                }
            }
        }

        std::sort(indice.begin(), indice.end(), cmp);

        for (int i = 0; i < choose - 1; i++)
        {
            printf("%d ", indice[i]+1);
        }
        printf("%d\n", indice[choose - 1] + 1);
    }
}

1032 The 3n + 1 problem

把之前计算过的结果扔到map里,遇到重复直接map查找
注意输入的两个数可能先大后小

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

std::map <long, long> a;


long cycleLen(long i)
{
    std::map <long, long>::iterator it = a.find(i);
    if (it != a.end())
        return it->second;

    if (i & 1)
    {
        long j = cycleLen(3 * i + 1) + 1;
        a[i] = j;
        return j;
    }
    else
    {
        long j = cycleLen(i / 2) + 1;
        a[i] = j;
        return j;
    }


}

void swap(long& i, long& j)
{
    long temp = i;
    i = j;
    j = temp;
}

int main()
{
    a[1] = 1;
    long start, end;
    while (scanf("%d%d", &start, &end) != EOF)
    {
        bool swapped = false;
        long maxLen = 0;
        if (start > end)
        {
            swap(start, end);
            swapped = true;
        }
        for (long i = start; i <= end; i++)
        {
            long len = cycleLen(i);
            maxLen = len > maxLen ? len : maxLen;
        }
        if(swapped)
            printf("%d %d %d\n", end, start, maxLen);
        else
            printf("%d %d %d\n", start, end, maxLen);
    }
}

1033 Edge

百度一下HDU1033就能查到题意

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

#define TOP 0
#define BOTTOM 2
#define LEFT 1
#define RIGHT 3

void move(char A, int& X, int& Y, int& dir)
{
    if (A == 'V')
        dir = (dir + 1) & 3;
    if (A == 'A')
        dir = (dir - 1) & 3;
    switch (dir)
    {
    case TOP:
        Y += 10;
        break;
    case BOTTOM:
        Y -= 10;
        break;
    case LEFT:
        X -= 10;
        break;
    case RIGHT:
        X += 10;
        break;
    default:
        break;
    }
    printf("%d %d lineto\n", X, Y);
}

int main()
{
    char a[210];
    while (scanf("%s", a) != EOF)
    {
        printf("300 420 moveto\n310 420 lineto\n");
        int len = strlen(a);
        int posX = 310, posY = 420;
        int dir = RIGHT;
        for (int i = 0; i < len; i++)
            move(a[i], posX, posY, dir);
        printf("stroke\nshowpage\n");
    }
}

1036 Average is not Fast Enough!

double四舍五入的实现方法:先+0.5再转成int

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

int main()
{
    int runner;
    double kilo;
    scanf("%d%lf", &runner, &kilo);
    int teamNumb;

    while (scanf("%d", &teamNumb) != EOF)
    {
        int sumHour = 0, sumMin = 0, sumSec = 0, sum;
        char t[10];
        bool flag = true;
        for (int i = 0; i < runner; i++)
        {
            scanf("%s", t);
            if (t[0] == '-')
                flag = false;

            sumHour += t[0] - '0';
            sumMin += (t[2] - '0') * 10 + t[3] - '0';
            sumSec += (t[5] - '0') * 10 + t[6] - '0';
        }
        if (flag)
        {
            sum = sumHour * 3600 + sumMin * 60 + sumSec;
            sum = (int)(sum / kilo + 0.5);

            sum %= 3600;
            printf("%3d: %d:", teamNumb, sum / 60);
            sum %= 60;
            printf("%02d min/km\n", sum);
        }
        else
            printf("%3d: -\n", teamNumb);
    }
}

1039 Easier Done Than Said?

水题,报错的话输入aa ae ee试一试

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

bool isVowel(char a)
{
    switch (a)
    {
    case 'a':
        return true;
    case 'e':
        return true;
    case 'i':
        return true;
    case 'o':
        return true;
    case 'u':
        return true;
    default:
        return false;
    }
}

bool acceptable(const char* passwd, int len)
{
    bool containVowel = false;

    if (len == 1)
    {
        return isVowel(passwd[0]);
    }

    if (len == 2)
    {
        if (passwd[0] == passwd[1])
            if (passwd[0] == 'e' || passwd[0] == 'o')
                return true;

        return isVowel(passwd[0]) || isVowel(passwd[1]);
    }

    containVowel |= isVowel(passwd[0]);
    containVowel |= isVowel(passwd[1]);

    char lastlastChar = passwd[0], lastChar = passwd[1];
    if (lastlastChar == lastChar)
        if (lastChar != 'e' && lastChar != 'o')
            return false;

    for (int i = 2; i < len; i++)
    {
        char thisChar = passwd[i];
        bool vowelFlag = isVowel(thisChar);
        containVowel |= vowelFlag;

        if (thisChar == lastChar)
            if (thisChar != 'e' && thisChar != 'o')
                return false;

        if (vowelFlag == isVowel(lastlastChar) && vowelFlag == isVowel(lastChar))
            return false;

        lastlastChar = lastChar;
        lastChar = thisChar;
    }

    if (containVowel)
        return true;
    else
        return false;
}

int main()
{
    char passwd[25];
    while (scanf("%s", passwd) != EOF)
    {
        if (!strcmp(passwd, "end"))
            break;

        int len = strlen(passwd);
        if (acceptable(passwd, len))
            printf("<%s> is acceptable.\n", passwd);
        else
            printf("<%s> is not acceptable.\n", passwd);
    }
}

1046 Gridland

趣味小数学

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

double grid(int a, int b)
{
    int c = a * b;
    if (c & 1)
        return (double)c + 0.41;
    else
        return (double)c;
}

int main()
{
    int num;
    scanf("%d", &num);

    for (int i = 0; i < num; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("Scenario #%d:\n%.2f\n\n", i + 1, grid(a, b));
    }
}
发布了17 篇原创文章 · 获赞 0 · 访问量 570

猜你喜欢

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