蓝桥杯2024B组

蓝桥杯 2024 国 B 立定跳远

二分查找

二分查找:通常用于在有序集合中快速查找到目标值。但当问题满足以下条件时,也可以使用二分查找来解决最优化问题:

解的范围明确且有序:需要确定解的可能范围,且该范围具有单调性。例如,当某个候选值满足条件时,更大(或更小)的值也一定满足条件。

存在高效的验证方法:能够快速判断某个候选值是否满足问题的条件。

cpp 复制代码
#include <iostream>
using namespace std;

int n, m;
int a[100010]; // 确保数组足够大以容纳原点和所有检查点

int check(int x) {
    int ans = 0;
    for (int i = 1; i <= n; i++) { // 处理所有相邻差,包括原点到第一个检查点
        int d = a[i] - a[i - 1];
        if (d > x) { // 仅当距离超过x时才需要分割
            ans += (d % x == 0) ? (d / x - 1) : (d / x);
        }
    }
    return ans <= m + 1; // 允许通过爆发技能减少一个检查点需求
}

int main() {
    cin >> n >> m;
    a[0] = 0; // 将原点0作为第一个元素
    for (int i = 1; i <= n; i++) {
        cin >> a[i]; // 输入的检查点存储在a[1]到a[n]
    }
    int l = 1, r = 1e8; // 左边界从1开始,避免除以零错误
    while (l < r) {
        int mid = (l + r) >> 1;
        if (check(mid)) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    cout << l << endl;
    return 0;
}

B: 小球反弹

下述代码分别使用cout和printf输出

cout用了科学计数法,其结果相同
要让 cout 的输出格式与 printf 一致(例如,以浮点数形式显示所有小数位),你需要手动控制 cout 的小数位数和输出格式(禁用科学计数法)。cout << fixed << setprecision(6) << value << endl; // 强制6位小数

cpp 复制代码
#include <iostream>
#include <vector>
#include<set>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
#include<map>
using namespace std;

int main(){
	for(long long i=2;i<10000;i+=2){
		for(long long j=2;j<10000;j+=2){
			if(343720*i*17==233333*j*15){
				cout<<sqrt((343720*i)*(343720*i)+(233333*j)*(233333*j))<<endl;
				printf("%f",sqrt((343720*i)*(343720*i)+(233333*j)*(233333*j)));
				return 0;
			}
		}
	}
return 0;
}

好数

cpp 复制代码
#include <iostream>
#include <vector>
#include<set>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
#include<map>
using namespace std;

int main(){
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		int num=i;
		for(int j=1;num;j++){
			if((num%10)%2==j%2)
				num/=10;
			else break; 
		}
		if(num==0)ans+=1;
	}
	cout<<ans;
return 0;
}

R 格式

cpp 复制代码
#include <iostream>
#include <vector>
#include<set>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
#include<map>
using namespace std;
int n;
double t;
int main(){
	cin>>n>>t;
	cout<<(unsigned long long)(t*(1ULL<<n)+0.5); 
return 0;
}

只能通过一半用例

相关推荐
嵌入式 Linux手记7 小时前
嵌入式面试都考的置位清位:位运算四件套
linux·c语言·驱动开发·面试·职场和发展·嵌入式
Pniubi17 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
Pniubi2 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
Pniubi2 天前
力扣239.滑动窗口最大值(优先队列)
算法·leetcode·职场和发展
一起学安全技术3 天前
拆大模型 11|大模型面试拷问:12 问逐层拆
人工智能·ai·语言模型·面试·职场和发展·求职招聘
CoderYanger4 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
希和20274 天前
代码小白笔记:蓝桥杯彩灯控制代码逐段讲解
笔记·mongodb·蓝桥杯
Pniubi4 天前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
Pniubi6 天前
力扣19 . 删除表的倒数第N个节点(栈)
算法·leetcode·职场和发展
杨逢昌工厂6S管理7 天前
150-杨逢昌一线现场诊断笔记:越管一线越抵触背后的熵增机理与基准化治理
经验分享·笔记·职场和发展·学习方法