寒假限时训练(1)A, B, C

· · 个人记录

A - Maximum GCD

#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t --)
    {
        int n;
        cin >> n;
        cout << n / 2 << endl;
    }

    return 0;
}

B - Black Square

#include <iostream>

using namespace std;

int main()
{
    int a[5];
    for (int i = 1; i <= 4; i ++)
        cin >> a[i];
    string s;
    cin >> s;
    int sum = 0;
    int f = s.length();
    for (int i = 0; i < f; i ++)
    {
        sum += a[s[i] - '0'];//通过数组的下标去访问'1'、'2'、'3'、'4'的值
    }
    cout << sum << endl;

    return 0;
}

C - Shortest Path with Obstacle

#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t --)
    {
        int x1, y1, x2, y2, xx, yy;
        cin >> x1 >> y1 >> x2 >> y2 >> xx >> yy;
        if (x1 == x2 && x1 == xx) //如果在一条竖线上
        {
            if ((yy > y1 && yy < y2) || (yy > y2 && yy < y1)) //若障碍在两个点中间,就需要避开
                cout << abs(y1 - y2) + 2 << endl;
            else //不在两个点之间那么直线走过去就可以了
                cout << abs(y1 - y2) << endl;
        }
        else if (y1 == y2 && y1 == yy)//在同一条横线上,同理
        {
            if ((xx > x1 && xx < x2) || (xx > x2 && xx < x1))
                cout << abs(x1 - x2) + 2 << endl;
            else
                cout << abs(x1 - x2) << endl;
        }
        else //如果两个点不在同一条直线上,那么怎么走最短距离都是一样的
            cout << abs(x1 - x2) + abs(y1 - y2) << endl;
    }

    return 0;
}