跳到正文

个人记录

河西-7.26上午3教室-廖

zmjzmj123456789约 8 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

T640773 T4

题意:

给出 n 个数,求出这 n 个数每个分别出现多少次?

错误代码

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
map <int,int> s;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a;
		cin>>a;
		if(s[a]!=0)
		{
			s[a]++;
		}
		else
		{
			s[a]=1;
		}
	}
	for(int i=1;i<=95000000;i++)
	{
		if(s[i]!=0)
		{
			cout<<i<<" "<<s[i]<<"\n";
		}
	}
	return 0;
}

错误地方:

C++
for(int i=1;i<=95000000;i++)
{
	if(s[i]!=0)
	{
		cout<<i<<" "<<s[i]<<"\n";
	}
}
遍历方式错误

正确代码

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
map <int,int> s;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a;
		cin>>a;
		s[a]++;
	}
	for(auto i=s.begin();i!=s.end();i++)
	{
		cout<<i->first<<" "<<i->second<<"\n";
	}
	return 0;
}

T640770 T6

题意:

给出 n 条绳子,问最少切成 k 段的情况下,最长可以切成的绳子长度。

错误代码

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
long double s[10005],cnt;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		double s;
		cin>>s;
		cnt+=(s/1*1.00);
	}
	cout<<fixed<<setprecision(2)<<cnt/k;
	return 0;
}

错误原因:

把所有的绳子连起来,剪断只会让结果增加。但是我们是一根一根的剪,所以代码错误。

正确代码

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#include<bits/stdc++.h>
#define ll long long
using namespace std;
double a[10005];
int n,k;
int f(double x,int s)
{
	for(int i=1;i<=n;i++)
	{
		s=s+a[i]/x;
	}
	if(s>=k)
	{
		return 1;
	}
	return 0;
}
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	double l=0,r=10001,mid;
	while(l+0.01<r)
	{
		mid=(l+r)/2;
		if(f(mid,0)==1)
		{
			l=mid;
		}
		else
		{
			r=mid;
		}
	}
	cout<<fixed<<setprecision(2);
	cout<<r;
	return 0;
}

代码解释:

f 函数用以判断 MID 是否可以满足至少切割 k 分的条件:
C++
int f(double x,int s)
{
	for(int i=1;i<=n;i++)
	{
		s=s+a[i]/x;
	}
	if(s>=k)
	{
		return 1;
	}
	return 0;//满足条件返回一,否则返回0
}
用二分查找法查找最接近答案的一个:
C++
double l=0,r=10001,mid;
while(l+0.01<r)
{
mid=(l+r)/2;
if(f(mid,0)==1)
{
	l=mid;
}
else
{
	r=mid;
}
}

错误原因:

方法错误。

T640769 T7

题意

给出仅由数字 0 与 1 组成的 n×n 格迷宫。若你位于一格 0 上,那么你可以移动到相邻 4 格中的某一格 1 上,同样若你位于一格 1 上,那么你可以移动到相邻 4 格中的某一格 0 上。询问最多可以走多少格子

正确代码

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
char a[1005][1005];
int py[]={-1,1,0,0},px[]={0,0,1,-1};
int n,m,cnt,t[1000005],v[1005][1005];
void dfs(int x,int y)
{
	v[x][y]=cnt;
	for(int i=0;i<4;i++)
	{
		int ny=y+py[i],nx=x+px[i];
		if (nx<1||ny<1||nx>n||ny>n);
		else if (v[nx][ny]!=0);
		else if (a[nx][ny]==a[x][y]);
		else dfs(nx,ny);
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(v[i][j]==0)
			{
				cnt++;
				dfs(i,j);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			t[v[i][j]]++;
		}
	}
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		cout<<t[v[x][y]]<<endl;
	}
	return 0;
}

代码解释:

dfs用于进行区分连通块:
C++
char a[1005][1005];
int py[]={-1,1,0,0},px[]={0,0,1,-1};
int n,m,cnt,t[1000005],v[1005][1005];
void dfs(int x,int y)
{
	v[x][y]=cnt;
	for(int i=0;i<4;i++)
	{
		int ny=y+py[i],nx=x+px[i];
		if (nx<1||ny<1||nx>n||ny>n);//判断是否越界
		else if (v[nx][ny]!=0);//这个位置已经是其他的联通块所属
		else if (a[nx][ny]==a[x][y]);
        //这个地方无法进行联通,也就是说这个地方和相邻的地方数值一样(0/0或1/1)
		else dfs(nx,ny);
	}
}

总结:

map函数的应用还需要加强,主要是其循环遍历以及循环中下标和值的变化。以及在题目中抓住关键信息和主要的算法。不能一味的使用模板,需要对其进行修改变形。
原文