寒假限时训练(1)V、W、X

· · 个人记录

V - Linear Keyboard

#include <bits/stdc++.h>

using namespace std;

typedef  long long ll;

int t;
char a[110];
char key[50];

int main()
{
    cin>>t;
    getchar();
    while(t--)
    {
        scanf("%s",key);
        scanf("%s",a);
        int len=strlen(a);
        int cnt=0;
        for(int i=1;i<len;i++)
        {
            int l,r;
            for(int j=0;j<26;j++)
            {
                if(key[j]==a[i-1])
                {
                    l=j;
                    break;
                }
            }
            for(int j=0;j<26;j++)
            {
                if(key[j]==a[i])
                {
                    r=j;
                    break;
                }
            }
            cnt+=abs(l-r);
        }
        cout<<cnt<<endl;
    }  
    return 0;
}

W - Stable Arrangement of Rooks

#include <bits/stdc++.h>

using namespace std;

int t;
int k, n;

int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        int flag = (n + 1) / 2; //最多能放的车的个数

        if (k > flag)
            cout << -1 << endl;
        else
        {
            int cnt = 0, x = 1;
            for (int i = 1; i <= n; i++)
            {
                if (i % 2 != 0)
                {
                    for (int j = 1; j <= n; j++)
                    {
                        if (j == x)
                        {
                            cnt++;
                            if (cnt <= k)
                                cout << "R";
                            else
                                cout << ".";
                        }
                        else
                        {
                            cout<<".";
                        }
                    }
                }
                else
                {
                    for(int j=1;j<=n;j++)
                    {
                        cout<<".";
                    }
                }
                x++;
                cout<<endl;
            }
        }
    }
    return 0;
}

X - Elections

#include <bits/stdc++.h>

using namespace std;

int t;
int a[5];

int main()
{
    cin >> t;
    while (t--)
    {
        int temp = 0;
        for (int i = 0; i < 3; i++)
        {
            cin >> a[i];
            temp = max(temp, a[i]);
        }

        int cnt = 0;
        for (int i = 0; i < 3; i++)
        {
            if (a[i] == temp)
                cnt++;
        }

        if (a[0] == a[1] && a[1] == a[2])
        {
            cout << "1 1 1" << endl;
        }
        else if (cnt == 2)
        {
            for (int i = 0; i < 3; i++)
            {
                if (a[i] == temp)
                    cout << "1";
                else
                    cout << temp - a[i] + 1;
                if (i == 2)
                    cout << endl;
                else
                    cout << " ";
            }
        }
        else
        {
            for (int i = 0; i < 3; i++)
            {
                if (a[i] != temp)
                    cout << temp - a[i] + 1;
                else
                    cout << 0;
                if (i == 2)
                    cout << endl;
                else
                    cout << " ";
            }
        }
    }
    return 0;
}