栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

第十二届蓝桥杯省赛 C/C++ B 组 暴力题解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第十二届蓝桥杯省赛 C/C++ B 组 暴力题解

人们都知道,蓝桥杯B组就是暴力比赛。。。

试题 A: 空间

1GB=1024MB 1MB=1024KB 1KB=1024B 1B=8b(位)

答案:67108864

cout<<256*1024*1024/4;
试题 B: 卡片

0-9每个都有2021张,记录每一张的剩余个数,然后从1开始枚举,知道卡片不够用结束,输出当前数字-1。

答案:3181

int main() {
	mapx;
	for(int i=0;i<=9;i++)x[i]=2021;
	for(int i=1;;i++){
		int n=i;
		while(n){
			if(x[n%10]==0){
				cout< 
试题 C: 直线 

斜率与截距确定一条直线,枚举出所有的直线,相差超过1e-8(10^ -8)就不是同一条直线了。

答案:40257

#include 
#include 
#include 
#include 

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;
}
试题 D: 货物摆放

就是找3个数乘积得2021041820210418的个数,根据题目可以发现,三个数都是当前数的约数,所以,先找出所有的约数,再枚举就行了。

答案:2340

int main() {
	seta;
	ll n=2021041820210418;
	for(ll i=1; i*i<=n; i++)
		if(n%i==0) {
			a.insert(i);
			a.insert(n/i);
		}
	int res=0;
	for(auto i:a)
		for(auto j:a)
			for(auto k:a)
				if(i*j*k==n)res++;
	cout< 
试题 E: 路径 

简单的最短路,利用Floyd -- Dijkstra...都可,因为是只提交答案,所以就用Floyd了

答案:10266837

int a[2025][2025];
int main() {
	for(int i=1;i<=2021;i++){
		for(int j=1;j<=2021;j++){
			if(abs(i-j)>21)
				a[i][j]=a[j][i]=0x3f3f3f3f;
			else {
				a[i][j]=a[j][i]=i*j/__gcd(i,j);
			}
		}
	}
	for(int k=1;k<=2021;k++){
		for(int i=1;i<=2021;i++){
			for(int j=1;j<=2021;j++)
			a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
		}
	}
	cout< 
试题 F: 时间显示 

注意:1s=1000ms 一天86400s 对86400求余即可

答案:

int main() {
	ll n;
	cin>>n;
	n/=1000;
	n%=86400;
	printf("%02d:%02d:%02d",n/3600,n%3600/60,n%3600%60);
}
试题 G: 砝码称重

不会DP就暴力求解 暴力二进制枚举拿一半分

暴力枚举:

int main() {
	int n;cin>>n;
	int res=0;
	setb;
	for(int i=0;i>a[i];
	for(int i=0;i<1<0)b.insert(sum);
		for(int j=0;j0)
			b.insert(sum-a[j]);
		}
	}
	cout< 

满分DP做法:

#include 
#include 
#include 

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("%dn", res);
    return 0;
}
试题 H: 杨辉三角形

20%还是可以拿的。。。。

int main(){
	int c[52]={0,1,5,8,12,17,13,30,38,47,18};
	int n;
	cin>>n;
	cout< 
试题 I: 双向排序 

自定义sort排序暴力做

#include
#include
#include
#include
#include
#include
const double pi=3.14159265358979323846;
typedef long long ll;
using namespace std;
int a[5005];
bool cmp1(int a,int b){
	return ab;
}
int main() {
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)a[i]=i;
	while(m--){
		int x,y;
		cin>>x>>y;
		if(x==1){
			sort(a+y,a+n+1,cmp1);
		}	
		else {
			sort(a+1,a+1+y,cmp2);
		}
	}
	for(int i=1;i<=n;i++)cout< 
试题 J: 括号序列 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号