目录
第一题:组队(手算)
第二题:年号字符(进制转换+模拟)
第三题:数列求值(轮转数组)
第五题:迷宫(bfs)
第六题:特别数的和(分解数字)
第七题:完全二叉树的权值(二叉树的遍历)
第八题:等差数列(数论:最大公约数)
第九题:后缀表达式(数学+贪心)
第十题:灵能传输(前缀和+贪心-->>太难了暂时放弃)
题目来源:
2019年第十届C/C++ B组蓝桥杯省赛真题_戎码一生的博客-CSDN博客_蓝桥杯c语言b组真题
我的评价是不如手算,(注意同一个人不能担任多个位置即可)
答案:490
核心思路:
将十进制转换为26进制,并转为大写字母(+64)输出
#include
#include
using namespace std;
int main()
{
int n = 2019;
stack s;
int temp;
while(n)
{
temp = n % 26;
s.push(temp);
n/=26;
}
while(!s.empty())//用栈更为方便:因为先进后出
{
char c = s.top()+64;
cout << c ;//答案:BYQ
s.pop();
}
return 0;
}
核心思路:
因为20190324这个数很大,由于空间受限且数字之间有规律,所以将其转为轮转数组,
为了下标对应,所以一开始是开的4个长度的数组,且对应1,2,3,数组的[0]位置其实初始化任意值即可,由于中间计算的过程中,值也有可能会爆,所以需要对进行取后四位数
#include
using namespace std;
int main()
{
int num[4] = { 0,1,1,1 };
int index = 4, k;
while (index <= 20190324)
{
k = index % 3;
if (k == 0)k = 3;
num[k] = (num[1] + num[2] + num[3]) % 10000;
index++;
}
cout << num[k];
return 0;
}
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
核心思路:
与平常的bfs不同的是,本题要求的走的路径,所以可以先用bfs计算出相邻格子距离差值为1的路径,然后遍历该路径,并且一定是要求题目所说的字典序来排序(在bfs中无所谓),在main函数中需要讲究先后顺序,所以dir数组,的顺序是‘D’,'U','L','R',如果满足相邻的各自距离为1,那么就将该字母字母加入string ans,然后break跳出,为的是只走一次,因为是一条完整的最短路径
#include
#include
#include
using namespace std;
typedef pair PII;
char g[30][50];
int dist[30][50];
char dir[] = { 'D', 'L', 'R', 'U' };
int dx[4] = { 0, 0, -1, 1 };
int dy[4] = { -1, 1, 0, 0 };
void bfs()
{
memset(dist, -1, sizeof dist);
queue q;
q.push({ 29, 49 });
dist[29][49] = 0;
while (q.size())
{
PII t = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int a = t.first + dx[i], b = t.second + dy[i];
if (a < 0 || a >= 30 || b < 0 || b >= 50) continue;
if (g[a][b] == '1' || dist[a][b] != -1) continue;
q.push({ a, b });
dist[a][b] = dist[t.first][t.second] + 1;
}
}
}
int main()
{
for (int i = 0; i < 30; i++) cin >> g[i];
bfs();
string ans;
int x = 0, y = 0;
while (x != 29 || y != 49)
{
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= 30 || b < 0 || b >= 50) continue;
if (g[a][b] == '1') continue;
if (dist[x][y] == dist[a][b] + 1)
{
ans += dir[i];
x = a, y = b;
break;
}
}
}
cout << ans << endl;
return 0;
}
#include
using namespace std;
bool check(int num)
{
while (num)
{
int j = num % 10;
if (j == 2 || j == 0 || j == 1 || j == 9) return true;
num /= 10;
}
return false;
}
int main()
{
int ans = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
if (check(i)) ans += i;
}
cout << ans << endl;
return 0;
}
#include
using namespace std;
const int N = 1e5 + 10;
long long q[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> q[i];
int maxv = -1e18;
int depth = 1;
int res = 1;
for (int i = 1; i <= n; i *= 2)
{
long long s = 0;
//完全二叉树 每层的开头为 2^(n-1) 结尾则是 2^n - 1
for (int j = i; j <= i * 2 - 1 && j <= n; j++)
{
s += q[j];
}
if (s > maxv)
{
maxv = s;
res = depth;
}
depth++;
}
cout << res << endl;
return 0;
}
具体解释在我的另一篇数论文章:
[AcWing蓝桥杯]之数论^ ^(C++题解)_lihua777的博客-CSDN博客
#include
#include
#include
#include
using namespace std;
const int N = 100010;
int a[N];
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
int d = 0;
for (int i = 1; i < n; i++) d = gcd(d, a[i] - a[0]);
if (!d) printf("%d\n", n);
else printf("%d\n", (a[n - 1] - a[0]) / d + 1);
return 0;
}
大佬的解法:
第十届蓝桥杯——后缀表达式_六级不考550+不改名-CSDN博客
核心思路:
后缀表达式:可以任意添加括号进行优先计算,所以可以把所有的负号,变成只有一个负号,那么就让负号对应那个最小的值,得到的总和就是最大的
#include
#include
#include
#include
using namespace std;
const int N = 200010;
typedef long long LL;
int n, m;
int a[N];
int main()
{
cin >> n >> m;
int k = n + m + 1;
for(int i = 0; i < k; i ++) scanf("%d", &a[i]);
LL ans = 0;
if(!m) // 减号的数量为 0
{
for (int i = 0; i < n + m + 1; i ++) ans += a[i];
}
else
{
sort(a, a + k);
ans = a[n + m] - a[0]; // 只保留一个减号
for (int i = 1; i < k - 1; i ++) ans += abs(a[i]);
}
cout << ans << endl;
return 0;
}
AcWing 1248. 灵能传输 - AcWing