P8598 [蓝桥杯 2013 省 AB] 错误票据
题目
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define INF 0x3f3f3f3f3f
const int N = 1000010;
using namespace std;
int arr[N];
signed main()
{
int N;
cin>>N;
int idx;
while(cin>>arr[++idx]){}
//排序
sort(arr+1,arr+idx+1);
int ans1,ans2;
for(int i = 1; i <= idx; i++){
if(arr[i+1] - arr[i] == 2)
ans1 = arr[i]+1;
if(arr[i+1] == arr[i])
ans2 = arr[i];
}
cout<<ans1<<' '<<ans2;
return 0;
}
P8752 [蓝桥杯 2021 省 B2] 特殊年份
题目
#include<bits/stdc++.h>
#define ll longlong
using namespace std;
int a[100010];
int main(){
int n = 5;
string s;
int ans = 0;
for(int i = 0; i < n;i++ ){
cin>>s;
if(s[0] == s[2]&& s[3] - s[1] == 1)
ans++;
}
cout<<ans;
return 0;
}
P8753 [蓝桥杯 2021 省 AB2] 小平方
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
int cnt= 0 ;
for(int i = 1; i<= n-1; i++){
int x = i*i;
if((x % n) < n/2.0){
cnt++;
}
}
cout<<cnt;
return 0;
}
P8780 [蓝桥杯 2022 省 B] 刷题统计
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b,n;
cin>>a>>b>>n;
int sum = 0;
int cnt = 1;
while(sum<n){
if(cnt % 7>=1&& cnt%7<=5)
sum+=a;
else
sum+=b;
cnt++;
}
cout<<cnt-1;
return 0;
}
P8706 [蓝桥杯 2020 省 AB1] 解码
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin>>s;
for(int i = 0;i < s.size();i++){
if(s[i]>='1'&& s[i]<='9')
for(int j = 0 ; j < s[i]-'1';j++)
cout<<s[i-1];
else
cout<<s[i];
}
return 0;
}
P8717 [蓝桥杯 2020 省 AB2] 成绩分析
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int a[N];
cin>>n;
for(int i = 0 ;i < n;i++){
cin>>a[i];
}
sort(a,a+n);
double sum = 0;
for(int i = 0 ; i < n;i++)
sum+= a[i];
double ans = sum/n*1.0;
cout<<a[n-1]<<endl<<a[0]<<endl;
printf("%.2lf",ans);
return 0;
}
P8711 [蓝桥杯 2020 省 B1] 整除序列
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n ;
cin>>n;
while(n>0){
cout<<n<<" ";
n = n>>1;
}
return 0;
}
P8722 [蓝桥杯 2020 省 AB3] 日期识别
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
unordered_map<string,int> mp{//创立哈希表,建立字符到整形的映射
{"Jan",1},
{"Feb",2},
{"Mar",3},
{"Apr",4},
{"May",5},
{"Jun",6},
{"Jul",7},
{"Aug",8},
{"Sep",9},
{"Oct",10},
{"Nov",11},
{"Dec",12}
};
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin>>s;
string s1 = s.substr(0,3);
string s2 = s.substr(3);
cout<<mp[s1]<<" ";
if(s2[0] != '0')
cout<<s2[0];
cout<<s2[1];
return 0;
}
P8680 [蓝桥杯 2019 省 B] 特别数的和
题目
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int ans = 0;
cin>>n;
for(int i = 1;i<=n;i++){
string s = to_string(i);
for(int j =0; j <s.size();j++){
if(s[j] == '0' || s[j] =='1'||s[j] =='2'||s[j] == '9')
{ ans+=i;
break;
}
}
}
cout<<ans;
return 0;
}
P9240 [蓝桥杯 2023 省 B] 冶炼金属
题目
- 思路 贪心
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
int a,b;
int ans_min = 0,ans_max = 1e9;
for(int i= 0; i< n;i++){
cin>>a>>b;
ans_min = max(ans_min,a/(b+1)+1);
ans_max = min(ans_max,a/b);
}
cout<<ans_min<<" "<<ans_max;
return 0;
}
P8597 [蓝桥杯 2013 省 B] 翻硬币
题目
- 思路 模拟
- 遇到不同的就反转
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f3f
using namespace std;
const int N = 100010;
int arr[N];
signed main(){
string a,b;
cin>>a>>b;
int cnt = 0;
for(int i = 0 ; i < a.size();i++){
if(a[i]!=b[i]){
a[i] = a[i] == 'o'?'*':'o';
a[i+1] = a[i+1] =='o'?'*':'o';
cnt++;
}
}
cout<<cnt;
return 0;
}
P8647 [蓝桥杯 2017 省 AB] 分巧克力
- 思路 打暴力超时 题干字眼——输出切出的正方形巧克力最大可能的边长 二分答案
- 对于第 i 块巧克力,当边长为 x 时,可以分出 ⌊(ai÷x)×(bi÷x)⌋ 块巧克力。
题目
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f3f
using namespace std;
const int N = 100010;
int a[N],b[N];//长宽
int n,k;
bool check(int mid){
int cnt =0 ;
for(int i = 1 ; i <= n ;i++){
cnt+=(a[i]/mid)*(b[i]/mid);
}
if(cnt>=k)return true;
else return false;
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i = 1; i <= n;i++){
cin>>a[i]>>b[i];
}
int l = 0;
int r = 10010;
while(l+1<r){
int mid = (l+r)>>1;
if(check(mid))
l = mid;
else
r = mid;
}
if(check(r)) cout<<r;
else cout<<l;
return 0;
}
P8772 [蓝桥杯 2022 省 A] 求和
题目
- 思路 前缀和 注意数据范围!
- S=a1⋅a2+a1⋅a3+⋯+a1⋅an+a2⋅a3+⋯+an−2⋅an−1+an−2⋅an+an−1⋅an
=(a2+a3+⋯+an)⋅a1+(a3+a4+⋯+an)⋅a2+⋯+an⋅an−1
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f3f
using namespace std;
const int N = 200010;
int arr[N],sum[N];
int n;
int ans;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i = 1 ; i<=n;i++){
cin>>arr[i];
sum[i] = sum[i-1] + arr[i];
}
for(int i =1 ;i <=n-1 ;i++){
ans += arr[i]*(sum[n]-sum[i]);
}
cout<<ans;
return 0;
}
P8665 [蓝桥杯 2018 省 A] 航班时间
题目
- 前置知识
-
- 去程时间 =飞行时间 + 时差;回程时间 = 飞行时间 − 时差
- 由此可知 飞行时间 = (去+回)/ 2
- 用 scanf 输入前面的时间:
scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
- 每一行后面不一定有额外的天数。如果有,则中间一定有空格,所以判断下一个字符是否为空格即可。
- 为方便计算,不妨把时间都转换成以秒为单位 t=86400⋅d+3600⋅h+60⋅m+s
- h = ans/3600 min = ans%3600/60 s = ans%60
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f3f
const int N = 1000010;
using namespace std;
int get()
{
int h1,m1,s1,h2,m2,s2,day=0;
scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
if(getchar()==' ')scanf("(+%d)",&day);
return (day*86400+h2*3600+m2*60+s2)-(h1*3600+m1*60+s1);
}
signed main()
{
int T;
scanf("%d",&T);
while(T--)
{
int ans=(get()+get())/2;
printf("%02d:%02d:%02d\n",ans/3600,ans%3600/60,ans%60);
}
return 0;
}
P8681 [蓝桥杯 2019 省 AB] 完全二叉树的权值
题目
- 思路 模拟
- 完全二叉树的性质
设深度为 dep,根节点的深度为 1。则有第 dep 层的节点为 2dep,每层开头的节点编号为 2dep−1,末尾的节点编号为 2dep−1(以上结论叶子节点除外)。
- 注意上面加粗黑体字 第一次没考虑到 错了两个点 叶子节点需要额外特判
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
int arr[N];
int n;
int dep = 1,sum = 0,Max = -1,a,ans;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i = 1; i <= n; i++){
cin>>a;
sum+=a;
if(i == (1<<dep)-1){//判断当前位置是否是该dep的最后一个节点?结算:go on
if(sum>Max){
Max = sum;
ans = dep;
}
dep++;
sum = 0;
}
}
if(sum>Max){//叶子节点特判
Max = sum;
ans = dep;
}
cout<<ans;
return 0;
}
P9231 [蓝桥杯 2023 省 A] 平方差
题目
- 思路 找规律
- 参考题解
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
int arr[N];
int n;
int f(int x) {//小于等于x的奇数个数
if (!x) return 0;
return (x + 1) / 2;
}
int g(int x) {//小于等于x的4的倍数个数
return x / 4;
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int l, r; cin >> l >> r;
cout << f(r) - f(l - 1) + g(r) - g(l - 1);
return 0;
}
P9230 [蓝桥杯 2023 省 A] 填空问题
题目
- 思路
- problem A 填空题 本地暴力枚举
- 把数字转成字符串去处理
- 答案为 4430091
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e8;
bool check(string s){
int ans1 = 0 ,ans2 = 0;
for(int i = 0; i < s.size();i++){
if(i<s.size()/2)
ans1 += s[i]-'0';
else
ans2 += s[i]-'0';
}
return ans1 == ans2;
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
int ans = 0;
for(int i = 1 ; i<= N;i++){
s= to_string(i);
if(s.size()%2 == 1)continue;//奇数
if(check(s)) {
ans++;
}
}
cout<<ans;
return 0;
}
- problem B DFS 本地暴搜
- 答案为 4165637
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e8;
int arr[N];
int ans;
void dfs(int score,int cnt){
if(cnt>30||score == 100)
return;
if(cnt == 30 && score == 70)
ans++;
dfs(score+10,cnt+1);
dfs(0,cnt+1);
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//表示0分 0题
dfs(0,0);
cout<<ans;
return 0;
}
P8649 [蓝桥杯 2017 省 B] k 倍区间
题目
题解参考
- 思路 看到”连续子序列求和”这一要求时,我们果断选择前缀和解答
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
int arr[N],sum[N];
int n,k;
map<int,int> mp;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
int ans = 0;
mp[0] = 1;
for(int i = 1; i<=n;i++){
cin>>arr[i];
sum[i] = (sum[i-1]+ arr[i])%k;//记录前缀和模k的值
mp[sum[i]]++;
}
for(int i = 0; i< n;i++){
ans+=(mp[i]*(mp[i]-1))/2;
}
cout<<ans;
return 0;
}
- 自己的理解
- 解释:1要和其他1组一起 满足
0可以单独算也可以组在一起 满足
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
int arr[N],sum[N];
int n,k;
map<int,int> mp;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
int ans = 0;
for(int i = 1; i<=n;i++){
cin>>arr[i];
sum[i] = (sum[i-1]+ arr[i])%k;//记录前缀和模k的值
mp[sum[i]]++;
}
for(auto i :mp){
if(i.first == 0)
ans+=i.second*(i.second+1)/2;
else
ans+=i.second*(i.second-1)/2;
}
cout<<ans;
return 0;
}