Codeforces 918(div4)

发布时间 2024-01-01 16:18:36作者: 不过是过客

Codeforces 918(div4)

Problem - A - Codeforces

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];

int main()
{
    long long n;
    cin >> n;

    while(n --)
    {
        int a , b , c;
        cin >> a >> b >> c;
        
        if(a == b) cout << c << endl;
        else if(a == c) cout << b << endl;
        else if(b == c) cout << a << endl;
    }
    return 0;
}

Problem - B - Codeforces

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int main()
{
    long long n;
    cin >> n;

    while(n --)
    {
        char a[4][4] = {0};
        int x , y;
        for(int i = 1 ; i <= 3 ; i ++)
        {
            for(int j = 1 ; j <= 3 ; j ++)
            {
                cin >> a[i][j];
                if(a[i][j] == '?')
                {
                    x = i;
                    y = j;
                }
            }
            
        }
        bool sta = false;
        bool stb = false;
        bool stc = false;
        for(int i = 1 ; i <= 3 ; i ++)
        {
            if(a[x][i] != '?')
            {
                if(a[x][i] == 'A') sta = true;
                if(a[x][i] == 'B') stb = true;
                if(a[x][i] == 'C') stc = true;
            }
            if(a[x][i] == '?') continue;
        }
        if(!sta) cout << "A" << endl;
        else if(!stb) cout << "B" << endl;
        else if(!stc) cout << "C" << endl;
    }
    return 0;
}

Problem - C - Codeforces

注意一下判断 是否为平方数的方法;也要记得开long long

#include<bits/stdc++.h>
using namespace std;

bool check(long long p)
{
    long long m = sqrt(p);
    return (long long)m * m == p;
}

int main()
{
    long long n;
    cin >> n;

    while(n --)
    {
        long long m;
        cin >> m;
        long long sum = 0;
        
        for(int i = 1 ; i <= m ; i ++) 
        {
            long long b;
            cin >> b;
            sum += b;
        }
        
        if(check(sum)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

Problem - D - Codeforces

正序写 讨论的情况比较多,所以选择倒叙看;

#include <bits/stdc++.h>
using namespace std;
char p[] = {'a','e'};
char q[] = {'b','c','d'};

int check1(char y) //判断是否为V
{
    for(int i = 0;i < 2;i++)
        if(p[i] == y) return 1;
    return 0;
}

int check2(char y)//判断是否为C
{
    for(int i = 0 ;i < 3;i++)
        if(q[i] == y) return 1;
     return 0;
}

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;
        
        string a;
        cin >> a;
        
        string ans;
        //倒着看情况少 好写代码
        //倒着看 只要第一个符合C那么就往前看是不是满足CVC
        //如果是V就看他前一个是不是C;
        for(int i = n - 1;i >= 0;i--) 
        {
            ans.push_back(a[i]);
            if(check2(a[i]) && check1(a[i-1]) && check2(a[i-2])) //如果符合CVC
            {
                ans.push_back(a[i-1]);
                ans.push_back(a[i-2]);
                ans.push_back('.');
                i -= 2;
            }
            
            else if(check1(a[i]) && check2(a[i-1])) //如果符合CV
            {
                ans.push_back(a[i-1]);
                ans.push_back('.');
                i -= 1;
            }
        }	
        
        for(int i = ans.size()- 2;i >= 0;i--) //要从ans.size()-2开始;
            cout << ans[i];
        
        cout << endl;
    }

    return 0;
}

Problem - E - Codeforces

根据题目要求 找出一段奇数的和 和 偶数的和相等的序列就输出yes,否则就输出no
a^l + a^(l+2) + ..+a^r = a^(l+1) + a^(l+3) +...+a^(r-1);
移项:
a^l - a^(l+1) + a^(l+2) - a^(l+3) +...+a^r-a^(r-1) = 0;
就找出这个式子;

也就是奇数项 - 偶数项 得到为0就代表YES
然后如果不是0 那么我们就要将这个数据记录下来,如果再次出现这个数据,那么就代表从出现过这个数据 后面的奇数项-偶数项的差值为0;那么也是输出YES
否则就输出no
#include <bits/stdc++.h>
using namespace std; 
const int N = 1e6 + 10;
int a[N];

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;
        
        for(int i = 1 ; i <= n ; i ++) cin >> a[i];
        
        long long sum = 0;
        map<long long , int>st;
        bool success = false;
        for(int i = 1 ; i <= n ; i ++)
        {
            if(i & 1) sum += a[i]; //奇数就加
            else sum -= a[i];//偶数就减
            
            if(sum == 0 || st[sum]) //sum等于0 或者 这个sum又出现过一次
            {
                cout << "yes" << endl;
                success = true;
                break;
            }
            else
            {
                st[sum] ++; //记录这个sum出现过
            }
        }
        if(!success) cout << "NO" << endl;
    }

    return 0;
}