蓝桥杯百校真题大联赛第4期(四)

A组

回文日期

#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;

using namespace std;
int res, res2;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool is_leap(int y, int m)
{
    if (m != 2) return false;
    return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}

bool check(int y, int m, int d)
{
    if (m < 1 || m > 12) return false;
    if (d < 1 || (d > months[m] + is_leap(y, m))) return false;
    return true;
}

int flip(int x)
{
    int res = 0;
    while (x)
    {
        res = res * 10 + x % 10;
        x /= 10;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    int y = n / 10000;
    for (int i = y; i <= 9999; i ++ )
    {
        int yy = i, mm = 0, dd = 0;
        
        int t = flip(yy);
        dd = t % 100;
        mm = t / 100;
        
        t = yy * 10000 + mm * 100 + dd;
        if (t <= n) continue;
        if (!res && check(yy, mm, dd)) res = yy * 10000 + mm * 100 + dd;
        
        if (mm == dd && dd % 10 != dd / 10 && check(yy, mm, dd)) 
        {
            res2 = yy * 10000 + mm * 100 + dd;
            break;
        }
    }
    printf("%d\n%d\n", res, res2);
    return 0;
}

子串分值和

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1e5 + 10;
char str[N];
typedef long long LL;
int pos[26];//记录每个字符最后一次出现的位置
int f[N];

int main() {
    scanf("%s", str + 1);
    int n = strlen(str + 1);
    LL ans = 0;
    for(int i = 1; i <= n; ++i) {
        int t = str[i] - 'a';
        f[i] = f[i - 1] + i - pos[t];
        pos[t] = i;
        ans += f[i];
    }

    printf("%lld\n", ans);
    return 0;
}

B组

砝码称重

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

using namespace std;

const int N = 110, M = 200010, B = M / 2;

int n;
int w[N];
bool f[N][M];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
    
    f[0][B] = true;
    for (int i = 1; i <= n; i ++ )
        for (int j = 0; j <= M ; j ++ )
        {
            if (f[i - 1][j])
            {
                if (j - w[i] >= 0) f[i][j - w[i]] = true;
                if (j + w[i] <= M) f[i][j + w[i]] = true;
                f[i][j] = true;
            }
        }
    
    int res = 0;
    for (int j = B + 1; j <= M; j ++ )
        res += f[n][j];
    
    cout << res << endl;
    
    return 0;
}

杨辉三角形

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

typedef long long LL;

using namespace std;

int n;

LL C(int a, int b)
{
    LL res = 1;
    for (int i = a, j = 1; j <= b; i --, j ++ )
    {
        res = res * i / j;
        if (res > n) return res;
    }
    return res;
}

bool check(int k)
{
    LL l = 2 * k, r = max(LL(n), l);
    while (l < r)
    {
        LL mid = l + r >> 1;
        if (C(mid, k) >= n) r = mid;
        else l = mid + 1;
    }
    if (C(r, k) != n) return false;
    
    cout << r * (r + 1) / 2 + k + 1 << endl;
    return true;
}

int main()
{
    cin >> n;
    for (int k = 16; ; k -- )
        if (check(k))
            break;
    return 0;
}

C组

次数差

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

using namespace std;

string s;
int main()
{
    unordered_map<char, int> hash;
    
    cin >> s;
    
    for (auto& c : s)
        hash[c] ++ ;
    
    int maxn = 0, minn = 1010;
    for (auto& [c, v] : hash)
    {
        maxn = max(maxn, v);
        minn = min(minn, v);
    }
    
    cout << maxn - minn << endl;
    
    return 0;
}

等腰三角形

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

using namespace std;
const int N = 610;

char g[N][N];

int main()
{
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n + i; j ++ )
            g[i][j] = '.';
    
    string ans = "";
    
    for (int i = 2; i <= 500; i ++ )
        ans += to_string(i);
    g[0][n - 1] = '1';
    int x = 1, y = n - 2, k = 0;
    
    int flag = 1;
    while (x != 0 || y != n - 1)
    {
        g[x][y] = ans[k ++ ];
        if (flag == 1)
        {
            if (x == n - 1)
            {
                y ++ ;
                flag = 2;
            }
            else
            {
                x ++ ;
                y -- ;
            }
        }
        else if (flag == 2)
        {
            if (y == 2 * n - 2)
            {
                flag = 3;
                x -- ;
                y -- ;
            }
            else y ++ ;
        }
        else 
        {
            x -- ;
            y -- ;
        }
    }
    
    for (int i = 0; i < n; i ++ )
        printf("%s\n", g[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值