HDU水题(6)

1076 An Easy Task

每400年有97个闰年,然后遍历

#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 isLeapYear(int Y)
{
    if ((Y % 4 == 0 && Y % 100 != 0) || Y % 400 == 0)
        return true;
    else
        return false;
}
int lastLeapYear(int Y)
{

    if (isLeapYear(Y))
        return Y;
    else 
        return lastLeapYear(Y-1);
}

int main()
{
    int numCase;
    scanf("%d", &numCase);
    for (int i = 0; i < numCase; i++)
    {
        int year, num;
        scanf("%d%d", &year, &num);
        year = lastLeapYear(year - 1);
        
        //97 leap years every 400 years
        int t = num / 97;
        year += 400 * t;
        num %= 97;

        while (num--)
        {
            if (isLeapYear(year + 4))
                year += 4;
            else
                year += 8;
        }
        printf("%d\n", year);
    }

}

1082 Matrix Chain Multiplication

建立一个栈,遇到矩阵丢进去,遇到右括号取出顶部两个矩阵相乘后将结果丢进去

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

class matrix
{
public:
    int line;
    int row;
    matrix(int l, int r):line(l),row(r){}
    matrix(matrix A, matrix B) :line(A.line), row(B.row) {}
    matrix():line(0),row(0){}
};

int mult(matrix A, matrix B)
{
    if (A.row == B.line)
        return A.line * A.row * B.row;
    else
        return 0;
}

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

    map<char, matrix> m;
    stack<matrix> s;
    while (num--)
    {
        char name;
        int line, row;
        getchar();
        scanf("%c %d %d", &name, &line, &row);
        m[name] = matrix(line, row);
    }

    char str[100];
    while (scanf("%s", str) != EOF)
    {
        if (str[0] != '(')
        {
            printf("0\n");
            continue;
        }

        int sum = 0;
        for (int i = 0; i < strlen(str); i++)
        {
            if (str[i] == '(')
                continue;
            else if (str[i] == ')')
            {
                matrix b = s.top();
                s.pop();
                matrix a = s.top();
                s.pop();
                if (!mult(a, b))
                {
                    printf("error\n");
                    goto C;
                }
                sum += mult(a, b);
                matrix c = matrix(a, b);
                s.push(c);
            }
            else
            {
                s.push(m[str[i]]);
            }

        }
        printf("%d\n", sum);
    C:;
    }

}

1084 What Is Your Grade?

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

using namespace std;

vector<string> solve4time, solve3time, solve2time, solve1time;

int solve4num = 0, solve3num = 0, solve2num = 0, solve1num = 0;

class student
{
public:
    int solve = 0;
    string time;
    int score = 0;


    student(int s,char* c):solve(s),time(c)
    {
        switch (s)
        {
        case 5:
            score = 100;
            break;
        case 4:
            solve4time.push_back(c);
            solve4num++;
            break;
        case 3:
            solve3time.push_back(c);
            solve3num++;
            break;
        case 2:
            solve2time.push_back(c);
            solve2num++;
            break;
        case 1:
            solve1time.push_back(c);
            solve1num++;
            break;
        case 0:
            score = 50;
            break;
        default:
            break;
        }
    }
    student(){}


};




bool cmp(string s1, string s2)
{
    if (strcmp(s1.c_str(), s2.c_str()) >= 0)
        return true;
    else
        return false;
}

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

        map<int, student> m;

        for (int i = 0; i < num; i++)
        {
            int s;
            char c[10];
            scanf("%d%s", &s, c);
            m[i] = student(s, c);
        }

        sort(solve4time.begin(), solve4time.end(), cmp);
        sort(solve3time.begin(), solve3time.end(), cmp);
        sort(solve2time.begin(), solve2time.end(), cmp);
        sort(solve1time.begin(), solve1time.end(), cmp);

        string avr4, avr3, avr2, avr1;
        if (solve4time.size())
            avr4 = solve4time[(solve4time.size() - 1) / 2];
        if (solve3time.size())
            avr3 = solve3time[(solve3time.size() - 1) / 2];
        if (solve2time.size())
            avr2 = solve2time[(solve2time.size() - 1) / 2];
        if (solve1time.size())
            avr1 = solve1time[(solve1time.size() - 1) / 2];

        for (map<int, student>::iterator it = m.begin(); it != m.end(); it++)
        {
            switch (it->second.solve)
            {
            case 4:
                if (cmp(it->second.time, avr4))
                    it->second.score = 90;
                else
                    it->second.score = 95;
                break;
            case 3:
                if (cmp(it->second.time, avr3))
                    it->second.score = 80;
                else
                    it->second.score = 85;
                break;
            case 2:
                if (cmp(it->second.time, avr2))
                    it->second.score = 70;
                else
                    it->second.score = 75;
                break;
            case 1:
                if (cmp(it->second.time, avr1))
                    it->second.score = 60;
                else
                    it->second.score = 65;
                break;
            default:
                break;
            }
        }

        for (int i = 0; i < num; i++)
        {
            printf("%d\n", m[i].score);
            //printf("%d %d %s\n", m[i].solve,m[i].score, m[i].time.c_str());
        }
        printf("\n");
        solve4time.clear(); solve3time.clear(); solve2time.clear(); solve1time.clear();
        solve4num = 0; solve3num = 0; solve2num = 0; solve1num = 0;
    }

}

1483 Automatic Correction of Misspellings

首先比较每个词与字典中的词的长度差,分情况讨论:
0:可能是拼写错误或者颠倒
1:多了一个字母
-1:少了一个字母

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

using namespace std;
#define unknown 0
#define misspelling 1
#define correct 2


bool missing(const char* less, const char* more)
{
    int i = 0;
    while (less[i] != '\0')
    {
        if (less[i] != more[i])
            break;
        i++;
    }
    while (less[i] != '\0')
    {
        if (less[i] != more[i + 1])
            return false;
        i++;
    }
    return true;

}

int samelength(const char* c, const char* d)
{
    int diff = 0, i = 0;
    while (c[i] != '\0')
    {
        if (diff > 2)
            return unknown;

        if (c[i] != d[i])
        {
            diff++;
            if (diff == 2)
            {
                if (c[i] != d[i - 1] || d[i] != c[i - 1])
                    return unknown;
            }
        }
        i++;
    }
    if (diff == 0)
        return correct;
    else if (diff == 1)
        return misspelling;
    else if (diff == 2)
        return misspelling;
    else
        return unknown;
}

void check(const char* c, const char* d, int& result, char* word)
{
    if (result == correct)
        return;

    else if (result == misspelling)
    {
        if (strlen(c) == strlen(d))
        {
            result = samelength(c, d) == correct ? correct : misspelling;
            if(result == correct)
                strcpy(word, d);
        }
    }

    else if (result == unknown)
    {
        switch (strlen(c) - strlen(d))
        {
        case 0:
            result = samelength(c, d);
            if(result)
                strcpy(word, d);
            break;
        case 1:
            if (missing(d, c))
            {
                result = misspelling;
                strcpy(word, d);
            }
            break;
        case -1:
            if (missing(c, d))
            {
                result = misspelling;
                strcpy(word, d);
            }
            break;
        default:
            break;
        }
    }
}

int main()
{
    int num ,result = unknown;
    char c[50],d[50],word[50];
    vector<string> dict;


    scanf("%d", &num);
    for (int i = 0; i < num; i++)
    {
        scanf("%s", c);
        dict.push_back(c);
    }

    scanf("%d", &num);
    for (int i = 0; i < num; i++)
    {
        scanf("%s", c);
        for (int j = 0; j < dict.size(); j++)
        {
            strcpy(d,dict[j].c_str());
            check(c, d, result, word);
        }

        if (result == correct)
            printf("%s is correct\n", c);
        else if (result == misspelling)
            printf("%s is a misspelling of %s\n", c, word);
        else if (result == unknown)
            printf("%s is unknown\n", c);

        result = unknown;
    
    }
}
发布了17 篇原创文章 · 获赞 0 · 访问量 567

猜你喜欢

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