明天就要去蓝桥杯比赛了
虽然是B组 但是还是要好好准(yu)备(xi)一下
目录
bitset
bitset 用于将字符串或者整数转换成 二进制数存储
需要添加头文件
#include <bitset>
PS: bitset存储中 下标是从右往左 从0开始计数的
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <bitset>
using namespace std;
int main()
{
bitset <8>a(13);
bitset <8>b(string("100101"));
// 存储位数足够 前面多余位用0补足
bitset <2>c(12);
bitset <4>d(string("100111"));
// 存储位数不够时 数字转换取后几位 字符串转换取前几位
cout << a[2] << endl;//注 :bitsetd 的下标是从右往左数的
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << endl;
string str = "10010111";
bitset <8> m(str);
cout << m << endl;
cout << m.count() << endl;// 返回m中1的个数
cout << m.size() << endl;// 返回m的大小
cout << m.test(2) << endl;// 检测下标为2的元素是否为1 若为1 则返回1
cout << m.any() << endl;// 检测m中是否有1
cout << m.none() << endl;// 检测m中是否没有1
cout << m.all() << endl;// 检测m中是否全部为1
cout << m.flip() << endl;// 所有位都取反
cout << m.flip(0) << endl;// 下标为0的位置取反
cout << m.set(2) << endl;// 将下标为2的元素置为1
cout << m.set() << endl;// 所有位都置为1
cout << m.set(1, 0) << endl;// 将下标为1的元素置为 0
cout << m.reset() << endl;// 将所有位置为0
//*执行 flip 、set 和 reset函数 后都会覆盖原有值
cout << endl;
bitset <8> n(str);
string s = n.to_string();
cout << s << endl;
unsigned long h = n.to_ulong();
cout << h << endl;
return 0;
}
运行结果如下:
参考博文:https://blog.csdn.net/vocaloid01/article/details/82798450
https://www.cnblogs.com/magisk/p/8809922.html
next_permutation
全排列函数 (将数组中所有数全排列)
(蓝桥杯历届真题大概出现了三四次这个函数的运用
添加头文件
#include <algorithm>
上代码(以前的笔记 输出 n 个数的全排列)
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <bitset>
using namespace std;
int a[1005];
int main()
{
int n;
while (~scanf("%d", &n)) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\n");
sort(a, a + n);
do {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
} while (next_permutation(a, a + n));
}
return 0;
}
输入输出结果如图:
注:如果将while循环放在大括号前 则会缺少最初输入的排列
while (next_permutation(a, a + n)){
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
输入输出结果
参考博文: https://www.cnblogs.com/eudiwffe/p/6260699.html
unique
这是一个去重函数 (同时将数组从小到大排序 重复出现的元素放到容器尾部)
mm = unique (a,a+n)-a;
返回去重后的数组大小
需要添加头文件:
#include <iostream>
#include <algorithm>
参考博文 : https://www.cnblogs.com/acm-bingzi/p/3330286.html
lower_bound
利用二分查找在已排序的数组中查找元素
lower_bound(begin,end,num) 找到第一个大于或等于num的数字 返回该数字的地址 否则返回end
通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。即 lower_bound(begin,end,num) - begin
upper_bound(begin,end,num) 找到第一个大于num的数字 返回该数字的地址 否则返回end
添加头文件
#include<algorithm>
参考博文 : https://www.cnblogs.com/Tang-tangt/p/9291018.html
噢~ 据说蓝桥杯是可以用万能头文件的 放一下 (but 部分OJ不能使用)
#include <bits/stdc++.h>
个人感觉 蓝桥杯的题目 大多与数学逻辑有关
而对代码的规范和使用要求较低
算法方面 需掌握深搜广搜 动态规划 递归等
祝大家都取得好成绩!!!ヾ(◍°∇°◍)ノ゙