飞道的博客

第十二届蓝桥杯C++赛后感

431人阅读  评论(0)

A 空间


32位程序中,INT变量占用4个字节
1mb=1024kb
1kb=1024B
1B=8b
B:byte
b:bit
32位二进制数是四个字节
实际上就是求256MB有多少个32 bit

答案:256*1024*1024/4
= 67108864

卡片


直接模拟即可

#include <bits/stdc++.h>

using namespace std;
#define ENDL "\n"
typedef long long ll;
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 10;

int a[10];

bool cal(int x) {
   
    while (x) {
   
        int y = x % 10;
        if (a[y])
            a[y]--;
        else
            return 0;
        x /= 10;
    }
    return 1;
}

int main() {
   
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for (int i = 0; i <= 9; i++) a[i] = 2021;
    for (int i = 1;; i++) {
   
        if (!cal(i)) {
   
            cout << i - 1 << endl;
            break;
        }
    }
    return 0;
}

答案:3181

直线


比赛时用map实现的,忘了自己做的对不对。。

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

using namespace std;

const int N = 200000;

int n;
struct Line
{
   
    double k, b;
    bool operator< (const Line& t) const
    {
   
        if (k != t.k) return k < t.k;
        return b < t.b;
    }
}l[N];

int main()
{
   
    for (int x1 = 0; x1 < 20; x1 ++ )
        for (int y1 = 0; y1 < 21; y1 ++ )
            for (int x2 = 0; x2 < 20; x2 ++ )
                for (int y2 = 0; y2 < 21; y2 ++ )
                    if (x1 != x2)
                    {
   
                        double k = (double)(y2 - y1) / (x2 - x1);
                        double b = y1 - k * x1;
                        l[n ++ ] = {
   k, b};
                    }

    sort(l, l + n);
    int res = 1;
    for (int i = 1; i < n; i ++ )
        if (fabs(l[i].k - l[i - 1].k) > 1e-8 || fabs(l[i].b - l[i - 1].b) > 1e-8)
            res ++ ;
    cout << res + 20 << endl;

    return 0;
}

我感觉当时做的答案好像是这个?

答案:40257

货物摆放


求n的约数,然后三重循环枚举,对n求约束,直接开方求就行,绝对够
(比赛时我是这么做的,确信)

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

using namespace std;

typedef long long LL;

int main()
{
   
    LL n;
    cin >> n;
    vector<LL> d;
    for (LL i = 1; i * i <= n; i ++ )
        if (n % i == 0)
        {
   
            d.push_back(i);
            if (n / i != i) d.push_back(n / i);
        }

    int res = 0;
    for (auto a: d)
        for (auto b: d)
            for (auto c: d)
                if (a * b * c == n)
                    res ++ ;
    cout << res << endl;

    return 0;
}
答案:2430

路径


就是一个建边跑最短路。。比赛时忘了gcd咋写emm

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

using namespace std;

const int N = 2200, M = N * 50;

int n;
int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[N];
bool st[N];

int gcd(int a, int b)  // 欧几里得算法
{
   
    return b ? gcd(b, a % b) : a;
}

void add(int a, int b, int c)  // 添加一条边a->b,边权为c
{
   
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void spfa()  // 求1号点到n号点的最短路距离
{
   
    int hh = 0, tt = 0;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q[tt ++ ] = 1;
    st[1] = true;

    while (hh != tt)
    {
   
        int t = q[hh ++ ];
        if (hh == N) hh = 0;
        st[t] = false;

        for (int i = h[t]; i != -1; i = ne[i])
        {
   
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
   
                dist[j] = dist[t] + w[i];
                if (!st[j])     // 如果队列中已存在j,则不需要将j重复插入
                {
   
                    q[tt ++ ] = j;
                    if (tt == N) tt = 0;
                    st[j] = true;
                }
            }
        }
    }
}


int main()
{
   
    n = 2021;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i ++ )
        for (int j = max(1, i - 21); j <= min(n, i + 21); j ++ )
        {
   
            int d = gcd(i, j);
            add(i, j, i * j / d);
        }

    spfa();
    printf("%d\n", dist[n]);
    return 0;
}
答案:10266837

时间显示


比赛时忘了1s等于多少ms,电脑自带计算器里有时间的进制关系(狗头🐕)

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

using namespace std;

typedef long long LL;

int main()
{
   
    LL n;
    cin >> n;
    n /= 1000;
    n %= 86400;
    int h = n / 3600;
    n %= 3600;
    int m = n / 60;
    int s = n % 60;
    printf("%02d:%02d:%02d\n", h, m, s);
    return 0;
}

G砝码称重


背包问题
自己对dp真的不熟。。五一要好好练练dp

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

using namespace std;

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

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

int main()
{
   
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]), m += w[i];

    f[0][B] = true;
    for (int i = 1; i <= n; i ++ )
        for (int j = -m; j <= m; j ++ )
        {
   
            f[i][j + B] = f[i - 1][j + B];
            if (j - w[i] >= -m) f[i][j + B] |= f[i - 1][j - w[i] + B];
            if (j + w[i] <= m) f[i][j + B] |= f[i - 1][j + w[i] + B];
        }

    int res = 0;
    for (int j = 1; j <= m; j ++ )
        if (f[n][j + B])
            res ++ ;
    printf("%d\n", res);
    return 0;
}

H杨辉三角形



枚举每一个斜列,然后二分找具体位置
思维题
妙啊,当时写了一个半暴力,真想不到

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

using namespace std;

typedef long long LL;

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)
{
   
	//C(a,b)
	//a>=2b,二分a 
    LL l = k * 2, r = n;
    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;
}

双向排列

讲解链接


转载:https://blog.csdn.net/qq_35975367/article/details/116276181
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场