蓝桥杯历届试题集合
回文数字
题目链接
思路:DFS尝试每一位
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; vector<int>ans; int a[10]; void dfs(int step , int s) { if(step == 6 || step == 7) { if(s == 0){ bool ok = true; if(a[1] == 0)ok = false; for(int i = 1 ; i <= step / 2 ; i ++){ if(a[i] != a[step - i]){ ok = false; break; } } if(ok){ int aa = 0; for(int i = 1 ; i <= step - 1 ; i ++){ aa = aa * 10 + a[i]; } ans.push_back(aa); } } if(step == 7)return ; } for(int i = 0 ; i <= 9 ; i ++){ if(i > s)break; a[step] = i; dfs(step + 1 , s - i); } } signed main() { ios; cin.tie(0);/// int n; cin >> n; dfs(1 , n); if(ans.size() == 0){ cout << -1 << '\n'; } else{ sort(ans.begin() , ans.end()); for(int i = 0 ; i < ans.size() ; i ++){ cout << ans[i] << '\n'; } } return 0; }
翻硬币
题目链接
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; #define debug(x) cout << #x << " : " << x << '\n'; vector<int>vec; char a[maxn] , b[maxn]; signed main() { ios; cin.tie(0);/// cin >> a + 1 >> b + 1; int n = strlen(a + 1); for(int i = 1 ; i <= n ; i ++){ if(a[i] != b[i])vec.push_back(i); } int ans = 0; for(int i = 0 ; i < vec.size() ; i += 2){ ans += vec[i + 1] - vec[i]; } cout << ans << '\n'; return 0; }
连号区间数
题目链接
思路:
看了一下觉得是个很难的题,没想到o(n2)可以过,大吃一斤...
在一个区间内如果这个区间的最大值减去最小值等于区间长度,即满足条件。
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; int a[maxn]; signed main() { ios; cin.tie(0);/// int n; cin >> n; for(int i = 1 ; i <= n ; i ++){ cin >> a[i]; } int ans = n; for(int i = 1 ; i <= n ; i ++){ int maxx = a[i] , minn = a[i]; for(int j = i + 1 ; j <= n ; j ++){ maxx = max(a[j] , maxx) , minn = min(minn , a[j]); if(maxx - minn == j - i) ans ++; } } cout << ans << '\n'; return 0; }
买不到的数目
思路:
找规律找出来的
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; signed main() { ios; cin.tie(0);/// int a , b; cin >> a >> b; cout << a * b - a - b << '\n'; return 0; }