第十二届蓝桥杯C++B组题解

2021/5/12 12:55:15

本文主要是介绍第十二届蓝桥杯C++B组题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A题

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

using namespace std;

//1MB = 1024KB 1KB = 1024B 1B = 8bit
int main(){
	cout<< 256 * 1024 * 1024 / (32/8)<<endl;
    return 0;
}

//67108864

B题

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

using namespace std;

int s[10];

bool check(int x)
{
	while (x)
	{
		s[x%10] -= 1;
		x = x / 10;
	}
	
	for (int i = 0; i < 10; i ++ )
		if (s[i] < 0)
		return false;
		
	return true;
}

int main(){
	for (int i = 0; i < 10; i ++ ) s[i] = 2021;
	
	for (int i = 0; ; i ++ )
	{
		if (!check(i))
		{
			cout<< i - 1;
			return 0;
		}
	}
    return 0;
}
//3181

C题

思路:数据量较小,四重循环暴力枚举,然后计算斜率和截距。合理的使用结构体存储以及定义重载和cmp规则排序。需要注意对于斜率无穷的直线特判(垂直x轴)。

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

//判断两个实数是否相等:若fabs(x1-x2)<1e-8则相等。 
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];

bool cmp(Line x,Line y)
{
	if (x.k != y.k) return x.k < y.k;
	return x.b < y.b;
}

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,cmp);
	int res = 1;//为什么是1?因为下面从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 

D题

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

using namespace std;

typedef long long LL; 

int main(){
	LL n;
	n = 2021041820210418;
	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

E题

裸的最短路问题,用SPFA、Dijkstra算法、Floyd(O(n^3),大概需要跑一分钟)。
没有背板子哭了。Y总用的SPFA,表示没学过。

#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)  // 欧几里得算法
{
	if(b == 0) return a; 
    return gcd(b, a % b);
}

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

F题

AcWing 3416 时间显示

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

using namespace std;

typedef long long LL;

const int D = 86400000;
const int H = 3600000;
const int M = 60000;
const int S = 1000;
int hh,mm,ss;

int main()
{
    LL n;
    cin >> n;
    n = n % D;
    hh = n / H;
    n = n % H;
    mm = n / M;
    n = n % M;
    ss = n / S;
    printf("%02d:%02d:%02d",hh,mm,ss);
}

G题

AcWing 3417 砝码称重

背包问题


                   

这篇关于第十二届蓝桥杯C++B组题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程