跳到正文

个人记录

第十五次训练课总结

hqj_约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年8月11日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 4 分钟

版本

  1. v1当前

第十四次训练课总结

这道题就是给定一个坐标(x, y),让我们输出同行、同列、同正对角线、同反对角线的所有坐标,

所以这道题我们可以直接输出。

C++
#include<bits/stdc++.h>
using namespace std;
long long n,x,y;
int main(){
	cin>>n>>x>>y;
	for(int i=1;i<=n;i++){//同行
		cout<<"("<<x<<","<<i<<")"<<" ";
	}
	cout<<endl;
	for(int i=1;i<=n;i++){//同列
		cout<<"("<<i<<","<<y<<")"<<" ";
	}
	cout<<endl;
	long long l=x,r=y;
	while(l!=1&&r!=1){//同正对角线
		r--;
		l--;
	}
	while(l<=n&&r<=n){
		cout<<"("<<l<<","<<r<<")"<<" ";
		r++;
		l++;
	}
	cout<<endl;
	l=x,r=y;
	while(l!=n&&r!=1){//同反对角线
		r--;
		l++;
	}
	while(l>=1&&r<=n){
		cout<<"("<<l<<","<<r<<")"<<" ";
		r++;
		l--;
	}
	return 0;
}

P8597 [蓝桥杯 2013 省 B] 翻硬币

这道题给定了一个硬币的序列,然后让我们翻硬币最终达成目标序列。

所以我们只需要从前往后枚举,找到不相同的硬币,那么就翻转这个硬币和她后面的一个硬币。

C++
#include<bits/stdc++.h>
using namespace std;
string x,y;
long long ans;
int main(){
	cin>>x>>y;
	for(int i=0;i<=x.size();i++){
		if(x[i]!=y[i]){//判断是否相同
			if(x[i]=='o'){//反转
				x[i]='*';
			}else{
                x[i]='o';
            }
			if(x[i+1]=='*'){
				x[i+1]='o';
			}else{
				x[i+1]='*';
			}
			ans++;
		}
	}
	cout<<ans;
	return 0;
}

P2681 众数

这道题就是让我们查询一个区间内出现次数最多的数字是谁。

找一个区间[x ~ y]中的众数可以直接从x ~ y枚举,找出里面出现次数最多的数字。

C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1005],f,ans,maxn;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	while(m--){
		int f,x,y;
		cin>>f>>x>>y;
		if(f==0){
			map<int,int>cnt;
			maxn=0;
			ans=0;
			for(int i=x;i<=y;i++){
				cnt[a[i]]++;// a[i]这个数字在x ~ y这个区间范围中出现了一次
				if(cnt[a[i]]>maxn){
					maxn=cnt[a[i]];
					ans=a[i];
				}else if(cnt[a[i]]==maxn&&a[i]<ans){
					ans=a[i];// a[i]才是众数,因为她出现的次数大于最大值
				}
                
			}
            cout<<ans<<endl;
		} else{
			a[x]=y;//将第x个位置的值修改为y
           
		}
		 
	}
	return 0;
}
原文