蓝桥杯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;
}

只能通过一半用例

相关推荐
_日拱一卒14 小时前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
jiayong2315 小时前
AI架构师面试题库 - 完整汇总文档
人工智能·面试·职场和发展
风筝在晴天搁浅17 小时前
美团 LeetCode 692.前K个高频单词
算法·leetcode·职场和发展
我爱cope18 小时前
【Agent智能体13 | 工具使用-什么是工具?】
人工智能·语言模型·职场和发展
he___H19 小时前
面试场景题
面试·职场和发展
ychqsq21 小时前
39.新年
经验分享·职场和发展
我命由我1234521 小时前
UGC、PGC、PUGC 极简理解
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
Komorebi_99991 天前
Day6:微调 vs RAG 场景区分(面试高频)
面试·职场和发展
英俊潇洒美少年1 天前
Vue2 $set 深度解析 + 批量更新全套优化方案(原理+实战+踩坑+面试)
面试·职场和发展·wps
飞天狗1111 天前
2025第十六届蓝桥杯c/c++B组国赛题解
c语言·c++·算法·蓝桥杯