第十二届蓝桥杯真题

发布时间:2026/5/25 7:22:57

第十二届蓝桥杯真题 1.简单的内存大小问题解题方法1kb1024B(字节)1B(字节)8位真题小蓝准备用 256MB 的内存空间开一个数组数组的每个元素都是 32 位 二进制整数如果不考虑程序占用的空间和维护内存需要的辅助空间请问 256MB 的空间可以存储多少个 32 位二进制整数解题#includebits/stdc.h using namespace std; int main(){ long long a256*1024*1024; long long b32/8; couta/b; return 0; }2.求最小公倍数a × b gcd(a, b) × lcm(a, b)gcd(a, b)是最大公约数lcm(a, b)是最小公倍数#includebits/stdc.h using namespace std; long long lcm(int a, int b) { // 第一步计算最大公约数辗转相除法 int x a, y b; while (y ! 0) { int temp y; y x % y; // 余数 x temp; // 除数变被除数 } int gcd x; // 最终 x 就是最大公约数 // 第二步用公式计算最小公倍数 return (long long)a / gcd * b; // 先除后乘避免溢出 } int main(){ long long a,b; cinab; long long clcm(a,b); coutc; return 0; }3.unordered_setint s{0};创建一个无序整数集合自动去重查找快#includebits/stdc.h using namespace std; int main(){ unordered_setint s{0}; //setint s{0};也可以但是这个顺序是固定的输出的结果是0 1 2 5 s.insert(1); s.insert(2); s.insert(5); s.insert(2); for(int x:s){ coutx ; }//输出结果是0 1 2 5(顺序不固定) return 0; }4.杨辉三角的特点#includebits/stdc.h using namespace std; int main(){ int n; cinn; if(n1){ cout1endl; return 0; } const int MAX1000; long long a[MAX][MAX]{0}; a[1][1]1; long long pos1; for(int i2;i35;i){ a[i][1]1; pos; for(int j2;ji;j){ a[i][j]a[i-1][j-1]a[i-1][j]; pos; if(a[i][j]n){ coutpos; return 0; } } } // 如果没找到说明n比较大出现在第n1行第2列 long long ans (long long)n * (n 1) / 2 2; cout ans endl; return 0; }在杨辉三角中每个正整数n第一次出现的位置要么在前34行内要么在第n1行第2列// 如果没找到说明n比较大出现在第n1行第2列 long long ans (long long)n * (n 1) / 2 2; cout ans endl;5.sort函数是左闭右开#includebits/stdc.h using namespace std; int main(){ vectorint a{6,3,5,4,1}; sort(a.begin(),a.begin()3); for(int j0;j5;j){ couta[j] ; }//输出结果是3 5 6 4 1 //而不是3 4 5 6 1 return 0; }降序排列还是左闭右开sort(vec.begin(), vec.end(), greaterint());#includebits/stdc.h using namespace std; int main(){ vectorint a{6,3,5,1,4}; sort(a.begin(),a.begin()3,greaterint()); for(int j0;j5;j){ couta[j] ; }//输出结果是6 5 3 1 4 //而不是6 5 3 4 1 return 0; }

相关新闻