杭电 ACM OJ 刷题笔记

2021-10-11 17:32:00
 尚寂新
这是一个创建于 263 天前的主题,其中的信息可能已经有所发展或是发生改变。

注:实时更新,仅记录学习,发来的都是 AC 的,可能不是最优解

HDU 2000

#include <iostream>
using namespace std;

void swap_char(char& a, char& b)
{
    char container = a;
    a = b;
    b = container;
}

int main()
{
    char a, b, c;
    while (cin >> a >> b >> c)
    {
        if (c < b) swap_char(c, b);
        if (c < a) swap_char(c, a);
        if (b < a) swap_char(b, a);
        
        cout << a << " " << b << " " << c << "\n";
    }

    return 0;
}

HDU 2001

被迫printf,用cout会 PE ,百度无果(现搜索到的无法通过编译,也有可能是因为我菜)
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main()
{
    float x1, y1, x2, y2;
    while (cin >> x1 >> y1 >> x2 >> y2)
    {
        //cout << sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) << "\n";
        printf("%.2f\n", sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
    }

    return 0;
}

HDU 2002

踩坑:float不够,需要double
#include <iostream>
#include <stdio.h>
#include <math.h>
#define PI 3.1415927
using namespace std;

int main()
{
    double r;
    while (cin >> r)
    {
        // V=(4/3)πr^3 
        printf("%.3lf\n", (4.0 / 3.0) * PI * r * r * r);
        //printf("%.3lf\n", (4.0 / 3.0) * PI * pow(r, 3));
    }

    return 0;
}

HDU 2003

这么简单,吓我一跳,以为还有坑...
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main()
{
    double num;
    while (cin >> num)
    {
        printf("%.2lf\n", abs(num));
    }

    return 0;
}

HDU 2004

感觉自己有点划水了
#include <iostream>
using namespace std;

int main()
{
    int range;
    while (cin >> range)
    {
        char score;
        if (range <= 100 && range >= 0)
        {
            if (range >= 90) score = 'A';
            else if (range >= 80) score = 'B';
            else if (range >= 70) score = 'C';
            else if (range >= 60) score = 'D';
            else score = 'E';

            cout << score << "\n";
        }
        else
        {
            cout << "Score is error!\n";
        }
    }

    return 0;
}

HDU 2005

AC 了,但感觉写的还是有些凌乱,思路有点飞
#include <iostream>
using namespace std;

int main()
{
    int y, m, d;
    while (cin >> y)
    {
        cin.ignore();
        cin >> m;
        cin.ignore();
        cin >> d;
        int dayNum = 0;

        for (int i = m - 1; i != 0; i--)
        {
            if (i == 2)
            {
                dayNum += y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;
            }
            else
            {
                dayNum += i % 2 == 0 ? 30 : 31;
            }
        }
        
        cout << dayNum + d << "\n";
    }

    return 0;
}

HDU 2006

要认真审题啊机芯,不要因为这么晚还在刷就分心了
#include <iostream>
using namespace std;

int main()
{
    int n;
    while (cin >> n)
    {
        int num[1000], count = 1;
        for (int i = 0; i < n; i++)
        {
            cin >> num[i];
            count *= num[i] % 2 != 0 ? num[i] : 1;
        }
        cout << count << "\n";
    }

    return 0;
}

HDU 2007

坑:测试数据中有的m是比n大的,需要做判断进行替换
#include <iostream>
using namespace std;

int main()
{
    int m, n;
    while (cin >> m >> n)
    {
        int x = 0, y = 0;

        if (m > n)
        {
            int container = m;
            m = n;
            n = container;
        }

        for (; m <= n; m++)
        {
            // 偶数平方和
            x += m % 2 == 0 ? m * m : 0 ;
            // 奇数立方和
            y += m % 2 != 0 ? m * m * m : 0 ;
        }
        cout << x << " " << y << "\n";
    }

    return 0;
}