• C++
  • csp-j-复赛模拟题参考答案

  • @ 2024-10-20 17:38:38

第一题:回转寿司

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int r, g, b;
    cin >> r >> g >> b;
    cout << r * 3 + g * 4 + b * 5 << endl;
    return 0;
}

第二题 杜萨和约比斯

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

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

    int x;
    while (cin >> x)
    {
        if (d > x) d += x;
        else break;
    }

    cout << d << endl;
    return 0;
}

第三题 铜牌数量

#include <iostream>

using namespace std;

const int N = 250010;
int a[N], s[N]; // s数组存放分数出现次数

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

    for(int i = 0; i < n; i ++)
    {
        scanf("%d", &a[i]);
        s[a[i]] ++; // 存放a数组元素出现次数
    }

    int res = 0;    // 存放排名第三的参赛者分数
    for(int i = 75, j = 0; i >= 0; i --)    // 从大到小遍历分数区间,j记录排名
    {
        if(s[i] != 0)  j ++;    // 如果该分数参赛者不为0,则该分段参赛者存在,j++
        if(j == 3)  // 检测到排名第三的参数者
        {
            res = i;
            break;
        }
    }

    printf("%d %d", res, s[res]);

    return 0;
}

第四题 问题按键

#include<bits/stdc++.h>

using namespace std;
string a,b;
bool is_jing(char c)//判断一个建是不是安静键
{
    string str;
    for(auto it : a)
        if(it != c)
            str += it;
    char t1,t2;
    for(int i = 0;i < str.size();i ++)
    {
        if(str[i] != b[i])
        {
            t1 = str[i];
            t2 = b[i];
            break;
        }
    }
    for(int i = 0;i < str.size();i ++)
    {
        if(str[i] != b[i])
        {
            if(!(str[i] == t1 && b[i] == t2))return false;
        }
    }
    return true;
}
int main()
{

    cin >> a >> b;

    if(a.size() == b.size())//长度相同找到第一个不同的字符就是答案
    {
        for(int i = 0;i < a.size();i ++)
        {
            if(a[i] != b[i])
            {
                cout<<a[i]<<" "<<b[i]<<endl;
                cout<<"-"<<endl;
                return 0;
            }
        }
    }
    //否则a b 长度不同
    char in_a[3],cnt = 0;//
    char in_b;//一定存在
    for(int i = 0;i < 26;i ++)
        {
            char c = char(i + 'a');
            if(a.find(c) != -1 && b.find(c) == -1)//字符c 在a中存在不在b中存在
            {
                in_a[++cnt] = c;
            }
            else if(a.find(c) == -1 && b.find(c) != -1)
            {
                in_b = c;
            }
        }
    int num_a1 = 0,num_a2 = 0,num_b = 0;
    for(int i = 0;i < a.size();i ++)
    {
        if(a[i] == in_a[1])num_a1 ++;
        if(a[i] == in_a[2])num_a2 ++;
    }
    for(int i = 0;i < b.size();i ++)
    {
        if(b[i] == in_b)num_b ++;
    }
    //上边的代码就是找到三个字符 在第一个字符串中找两个不在第二个字符串中出现的字符
    //同时在第二个字符串中找一个不在第一个字符串中出现的字符
    // 前两个字符中一定有一个时安静键,可以写一个函数判断
    // 其余两个字符就是fool键,可以直接输出
    //后边可能看起来有点麻烦
    if(num_a1 == num_a2)
    {
        if(is_jing(in_a[1]))
        {

            cout<<in_a[2]<<" "<<in_b<<endl;
            cout<<in_a[1]<<endl;
        }else
        {
            cout<<in_a[1]<<" "<<in_b<<endl;
            cout<<in_a[2]<<endl;
        }
    }
    else
    {
        if(num_a1 == num_b)
        {
            cout<<in_a[1]<<" "<<in_b<<endl;
            cout<<in_a[2]<<endl;
        }else
        {
            cout<<in_a[2]<<" "<<in_b<<endl;
            cout<<in_a[1]<<endl;
        }
    }
    return 0;
}

第五题 收获滑铁卢

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

int n, m;
vector<string> g;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

int get_score(char c)
{
    if (c == 'S') return 1;
    if (c == 'M') return 5;
    return 10;
}

int dfs(int x, int y)
{
    int res = get_score(g[x][y]);
    g[x][y] = '*';

    for (int i = 0; i < 4; i ++ )
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= m || g[a][b] == '*') continue;
        res += dfs(a, b);
    }

    return res;
}

int main()
{
    cin >> n >> m;
    g.resize(n);
    for (int i = 0; i < n; i ++ ) cin >> g[i];

    int x, y;
    cin >> x >> y;
    cout << dfs(x, y) << endl;

    return 0;
}

0 条评论

目前还没有评论...