2020 11th Blue Bridge Cup C/C++ Provincial Competition Group A Problem Solutions

A

Insert picture description here

624
traverse each number, take the remainder to judge each bit

#include<iostream>
using namespace std;

int main()
{
    
    
	int cnt=0;
	for(int i=1;i<=2020;i++)
	{
    
    
		int tmp=i;
		while(tmp)
		{
    
    
			if(tmp%10==2)
				cnt++;
			tmp/=10;
		}
	}
	cout<<cnt;
	return 0;
}

B


2481215
two cycles to determine whether the greatest common divisor is 1

#include<iostream>
using namespace std;

int gcd(int a,int b)
{
    
    
	if(b==0)
	return a;
	else 
	return gcd(b,a%b);	
}

int main()
{
    
    
	int cnt=0;
	for(int i=1;i<=2020;i++)
		for(int j=1;j<=2020;j++)
			if(gcd(i,j)==1)
				cnt++;
	cout<<cnt;
	return 0;
}

C

Insert picture description here
761
Finding the law directly is faster
(1,1) 1=0+1
(2,2) 5=(1+2)+2
(3,3) 13=(1+2+3+4)+3
(i , I) x=(1+2+3+…+2(i-1))+i=(i-1)(2i-1)+i
with serpentine filling code

#include<iostream>
using namespace std;

int a[1001][1001];

int main()
{
    
    
	int cnt=1;
	int i=1,j=1;
	a[i][j]=cnt;
	while(cnt<10000)
	{
    
    
		
		if(i==1)
			j++;
		a[i][j]=++cnt;
		while(j>1)
			a[++i][--j]=++cnt;
		if(j==1)
			i++;
		a[i][j]=++cnt;
		while(i>1)
			a[--i][++j]=++cnt;
	}
	cout<<a[20][20];
	return 0;
}

D

Insert picture description here
80
building maps, dfs. Each light can be on or off, enumerate 2^7.

#include<iostream>
using namespace std;

const int N=8;
int map[N][N],fa[N],vis[N],ans;

int getfa(int i)										//寻找根节点 
{
    
    
	if(fa[i]==i)
		return i;
	return fa[i]=getfa(fa[i]);							//压缩路径 
}

void dfs(int k)
{
    
    
	if(k>7)												//7个灯都枚举完了 
	{
    
    
		for(int i=1;i<=7;i++)							//先把自己设为自己的父节点 
			fa[i]=i;
		for(int i=1;i<=7;i++)
			for(int j=1;j<=7;j++)
			{
    
    
				if(map[i][j]&&vis[i]&&vis[j])			//将连通且亮着的灯,并在一块 
				{
    
    
					int faa=getfa(i),fab=getfa(j);
					if(faa!=fab)
						fa[faa]=fab;
				}
			}
		int cnt=0;
		for(int i=1;i<=7;i++)							//有几个根节点说明有几个集合 
			if(fa[i]==i&&vis[i])						//亮着的灯的集合数 
				cnt++;
		if(cnt==1)										//都连通的话,只有一个集合 
			ans++;							
		return;
	}
	vis[k]=1;											//k灯亮  dfs中k+1亮和灭 
	dfs(k+1);
	vis[k]=0;											//k灯灭 
	dfs(k+1);
}

int main()
{
    
    
	map[1][2]=map[2][1]=1;map[1][6]=map[6][1]=1;	//连通的存图 
	map[6][7]=map[7][6]=1;map[6][5]=map[5][6]=1;
	map[2][7]=map[7][2]=1;map[2][3]=map[3][2]=1;
	map[5][7]=map[7][5]=1;map[5][4]=map[4][5]=1;
	map[3][7]=map[7][3]=1;map[4][3]=map[3][4]=1;
	dfs(1);
	cout<<ans;
	return 0;
}

E

Insert picture description here
1391
Between arbitrary lines (including straight lines and curves) that do not intersect at the same point, the plane can be divided into as many parts as possible.
According to Euler's theorem V−E+F−T=1, T=0, solve for F =1+E−V, you only need to solve for the number of points V and the number of sides V.
According to our analysis, any two straight lines can have an intersection, the number is (202); there are two intersections between any two circles, The number is 2⋅(202); there are two intersection points between an arbitrary circle and a straight line, the number is 20×20×2,
so V=(202)+2⋅(202)+20×20×2=1370 The
same is true In our analysis, a straight line is intersected by the remaining 19 straight lines at one point each, and 20 circles are intersected at two points each, so there are 19+20×2=59 points on the line, and a straight line is divided into 60 sides; A circle is intersected by the remaining 19 circles and 20 straight lines at two points each, a total of (19+20)×2=78 points, a circle is divided into 78 sides,
so E=60×20+78×20= 2760
therefore gets the answer: F=1+2760−1370=1391

F

Insert picture description here
Insert picture description here

#include<iostream>
#include<cstdio>
using namespace std;

int a[100001];

int main()
{
    
    
	int n,maxx=-1,minn=101;
	cin>>n;
	long long s=0;
	double ave;
	for(int i=1;i<=n;i++)
	{
    
    
		scanf("%d",&a[i]);
		s+=a[i];
		maxx=max(maxx,a[i]);
		minn=min(minn,a[i]);
	}
	ave=s/double(n);
	printf("%d\n%d\n%.2lf",maxx,minn,ave);	
}

G

Insert picture description here
Insert picture description here
Judge each date, take the remainder of the date, and store each bit in the array, and judge whether the corresponding is equal or not.
Pay attention to judging that the date is legal

#include<iostream>
using namespace std;

int main()
{
    
    
	int n,ans1=0,ans2=0;
	int a[10];
	cin>>n;
	for(int i=n+1;;i++)
	{
    
    
		int j=1;
		int tmp=i;
		while(j<=8)
		{
    
    
			a[j++]=tmp%10;
			tmp/=10;
		}
		bool flag=0;
		int m=a[5]*10+a[6],k=a[7]*10+a[8],y;
		switch(m)
		{
    
    
			case 4:
			case 6:
			case 9:
			case 11:
				if(m>30) flag=1;
				break;
			case 2:
				y=a[1]*1000+a[2]*100+a[3]*10+a[4];
				if((y%4==0&&y%100!=0)||y%400==0) {
    
    if(m>29) flag=1;break;}
				else {
    
    if(m>28) flag=1;break;}
			default:
				if(m>31) flag=1;
				break;	
		}
		if(flag)
			continue;
		if(a[1]==a[3]&&a[1]==a[6]&&a[1]==a[8]&&a[2]==a[4]&&a[2]==a[5]&&a[2]==a[7])
			for(int j=8;j;j--)
			{
    
    
				ans2*=10;
				ans2+=a[j];
			}
		if(a[1]==a[8]&&a[2]==a[7]&&a[3]==a[6]&&a[4]==a[5]&&!ans1)
			for(int j=8;j;j--)
			{
    
    
				ans1*=10;
				ans1+=a[j];
			}
		if(ans1&&ans2)
			break;	
	}
	cout<<ans1<<endl<<ans2;
	return 0;
}

H

Insert picture description here
Insert picture description here
It can be enumerated, triple loop, so that n needs to be less than 100, obviously not the
number of substrings contributed by each letter, the absolute value of its subscript subtracted from the previous same letter subscript and the next same letter subscript respectively Multiply, and then accumulate each letter is the answer. For
Insert picture description here
example, this string in the figure, count the number of contribution substrings of position 4 a, the
first a subscript is 1, and the last one is 6 (4-1)*(6 -4)=6
Explain that 4-1 corresponds to bca (No. 234) and 6-4 corresponds to ab (No. 45)
. The substrings with bca as the starting position and ba as the ending position all contain only the 4th a, and It is a substring
that contains only the 4th a, which is bca bcab ca cab a ab.
Note that at the beginning, there may not be the same letter before or after

#include<iostream>
#include<string>
using namespace std;

const int N=1e5+5;
int pre[N],nex[N],a[27];
string s;

int main()
{
    
    
	cin>>s;
	s="0"+s;						//下标从1开始 
	int n=s.length()-1;				//长度 
	for(int i=1;i<=n;i++)			//寻找前一个相同字符下标 
	{
    
    
		int c=s[i]-'a';
		pre[i]=a[c];
		a[c]=i;
	}
	for(int i=0;i<26;i++)			//初始化,可能后边没有相同字符    寻找前一个时,未初始,因为a数组默然为0 
		a[i]=n+1;
	for(int i=n;i;i--)				//寻找后一个相同字符下标 
	{
    
    
		int c=s[i]-'a';
		nex[i]=a[c];
		a[c]=i;
	}
	long long int ans=0;
	for(int i=1;i<=n;i++)
		ans+=(long long)(i-pre[i])*(nex[i]-i);//相乘计算累加
	cout<<ans; 
	return 0;
}

I

Insert picture description here
Insert picture description here
Find the area of ​​the overlapping part of the ellipse and the triangle, forgive me not

J

Insert picture description here
Insert picture description here
Uh uh uh, still no,
who will teach me orz

Guess you like

Origin blog.csdn.net/m0_54621932/article/details/114024665