第十二届蓝桥杯B组题目及题解

本文主要介绍了第十二届蓝桥杯B组竞赛的填空题和编程题。在填空题部分,讨论了精度问题、大数的公约数计算以及最短路算法,提到了Dijkstra、SPFA和Floyd算法。在编程题中,重点讲解了砝码称重问题,涉及有选择的背包问题,通过动态规划(DP)解决,提出了不正宗但能通过的数据范围限制解法,并提及了正宗的组合数解法。
摘要由CSDN通过智能技术生成

== 蓝桥杯B组(java 或者C)==

填空题

前两道填空题就不说了

在这里插入图片描述
这道题大多数人应该都是卡到了精度问题 double比较大小小于一定的精度我们就认为已经相等了直接上代码

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

using namespace std;

typedef pair<double, double> PII;



PII l[200000];

int main(){
   
   
   int n = 0; 
   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].first - l[i - 1].first) > 1e-8 || fabs(l[i].second - l[i - 1].second) > 1e-8)
           res ++;
    }
    
    cout << res + 20 <<endl;
    答案 :40257
}

在这里插入图片描述
这一道题考察的就是大数的公约数

#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 
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值