上海月赛-9月丙组题参考代码

T1

#include <bits/stdc++.h>

using namespace std;

bool isPerfectSquare(int n)
{
    int root = sqrt(n);
    return root * root == n;
}

int main()
{
    int n;
    cin >> n;
    if(isPerfectSquare(n))
    {
        cout << n;
        return 0;
    }

    for(int i = n/2; i >= 1; i--)
    {
        if(0 == n % i && isPerfectSquare(i))
        {
            cout << i;
            break;
        }
    }

    return 0;
}

T2

#include <bits/stdc++.h>

using namespace std;

bool isPerfectSquare(int n)
{
    int root = sqrt(n);
    return root * root == n;
}

int main()
{
    int n;
    cin >> n;
    if(isPerfectSquare(n))
    {
        cout << n;
        return 0;
    }

    for(int i = n/2; i >= 1; i--)
    {
        if(0 == n % i && isPerfectSquare(i))
        {
            cout << i;
            break;
        }
    }

    return 0;
}

T3

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

const int maxM = 1005;
struct course
{
    string className;
    int capacity;
}cour[maxM];

int main()
{
    int m, n;
    cin >> m >> n;
    for(int i = 1; i <= m; i++)
    {
        cin >> cour[i].className >> cour[i].capacity;
    }

    map<string, int> cnt;
    string name;
    for(int i = 1; i <= n; i++)
    {
        cin >> name;
        cnt[name]++;
    }

    int classCnt = 0, studentCnt = 0;
    for(int i = 1; i <= m; i++)
    {
        if(cnt[cour[i].className] * 2 < cour[i].capacity)
        {
            classCnt++;
            studentCnt += cnt[cour[i].className];
        }
        else if(cnt[cour[i].className] > cour[i].capacity)
        {
            studentCnt += cnt[cour[i].className] - cour[i].capacity;
        }
    }

    cout << classCnt << endl << studentCnt;

    return 0;
}

T4

#include <iostream>
using namespace std;

int main()
{
    string s;
    cin >> s;
    int len = s.size();
    for(int i = 0; i < len - 1; i++)
    {
        if(s[i] == s[i + 1] || s[i] == s[i + 2])
        {
            cout << "Not a random string";
            return 0;
        }
    }

    cout << "Random string";
    return 0;
}

T5

#include <iostream>

using namespace std;

const int mod = 1e9 + 9;
const int maxN = 1e6 + 5;
int dp[maxN];


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

    dp[0] = 1;
    dp[1] = 2;
    for(int i = 2; i <= n; i++)
    {
        dp[i] = (dp[i - 1] + dp[i - 2] ) % mod;
    }

    cout << dp[n];

    return 0;
}

==============================
长按识别二维码加好友邀请进信息学竞赛群
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值