2021 ZZUSOFT freshman competition (Game 1)

2021 ZZUSOFT freshman competition (Game 1)

Title Link

Note: the data type range is reflected in many topics

A

Directly output the corresponding ASCII code

'A' + 0 representative'A'
'A' + 1 representative'B'
#include<stdio.h>
#include<math.h>

int main()
{
	for(int i=1;i<=26;i++)
	{
		int x;
		scanf("%d",&x);
		printf("%c",'A'+x-1);
	}
	return 0;
}

B

Binary to decimal

  • Method 1: the same factor 2 can be proposed and finally transformed into the following formula.
#include<stdio.h>
#include<string.h>
char s[100];

int main()
{
	long long sum = 0;
	scanf("%s",s);
	int len = strlen(s);
	for(int i=0;i<len;i++)
		sum = sum * 2 + s[i]-'0';
	printf("%lld\n",sum);
} 
  • Method 2: convert from the lowest bit to decimal according to the definition of binary
#include<stdio.h>
#include<string.h>
char s[66];
int main()
{
	long long res = 0;
	scanf("%s",s);
	int len = strlen(s);
	long long x = 1;
	for(int i=len-1;i>=0;i--,x *= 2)
		res += (s[i]=='1') * x;
	printf("%lld\n",res);
	return 0;
}

C

Scissors, stone, paper, list all the situations. To simplify the procedure, only the first letter is taken for comparison.

#include<stdio.h>
#include<string.h>
char a[10],b[10];

int main()
{
	scanf("%s%s",a,b);
	if((a[0]=='s' && b[0]=='s') ||
	 (a[0]=='r' && b[0]=='r') || (a[0]=='p' && b[0]=='p'))
		printf("D\n");
	else if((a[0]=='s' && b[0]=='p')||
	(a[0]=='r' && b[0]=='s')||(a[0]=='p'&& b[0]=='r'))
		printf("L\n");
	else printf("S\n");
} 

D

Special cases are listed separately, then the slope k is calculated according to the slope formula, and then b is calculated.

#include<stdio.h>
#include<string.h>

int main()
{
	int x1,y1,x2,y2;
	scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
	if(x1 == x2)
	{
		printf("x=%.6lf\n",x1);
		return 0;
	}
	double k = ((double)y2-y1)/(x2-x1);
	double b = y1-k*x1;
	printf("%.6lf %.6lf\n",k,b);
	return 0;
} 

E

Directly corresponding to the output

#include<stdio.h>
#include<string.h>

int main()
{
	int n;
	char s[10];
	scanf("%d%s",&n,s);
	if(s[n-1]=='s') printf("YES\n");
	else printf("NO\n");
} 

F

Simplified formula ∣ x + 1 ∣ |x+1| ∣x+1∣

Cannot be calculated directly x 2 + 2 x + 1 x^2+2x+1 x2+2x+1, explosive data range

When x is int and x*x is int, if the multiplied result is greater than the range of int, it will overflow

#include<stdio.h>
#include<math.h>
int main()
{
	long long x;
	scanf("%lld",&x);
    //printf("%lld\n",abs(x+1));
    if(x<-1) printf("%lld\n",-x-1);
    else printf("%lld\n",x+1);
	return 0;
 } 

G

There are two situations when a point moves from the origin to another point:

  1. The walking radius is greater than the distance between two points. It only takes two steps to reach the target point. The path of these two parts is just the two waist of isosceles triangle, and the line segment formed by the connection of two points is the bottom edge of isosceles triangle

  2. The walking radius is less than the distance between two points. Note that the distance between two points is dis and the radius is r

    be d i s = k ∗ r + x dis = k*r + x dis=k * r + x, K is an integer, x is a number, satisfying x < r x < r x<r.

    If x is 0, the result is naturally k

    If x is not 0, the radius of the previous integer multiple can go k-1 step. The latter x only needs to combine the last step of the previous integer multiple radius to reach the end point (as in the first case)

    The second case is to find the upper rounding of dis/r

#include<stdio.h>
#include<math.h>

int main()
{
	int r,x,y;
	scanf("%d%d%d",&r,&x,&y);
	double dis = sqrt((double)x*x+(double)y*y);
	if(r>dis) printf("2\n");
	else printf("%d\n",(int)ceil(dis/r)); 
	return 0;
}

H

Find a right triangle whose right angle side is parallel to the line where the row and column are located.

Consider starting with the right vertex of a right triangle.

The point in the row where the right angle vertex is located can form a point of the right triangle, and the point in the column can form a point of the right triangle. The number of points that can be selected in the current row is the number of points of 1 in the current row minus one, and the number of points that can be selected in the current column is the number of points of 1 in the current row minus one. Therefore, we only need to count each row (row []) and each column (col []) Just as many points.

Then loop through each point, and if the current point is 1, add to the answer ( r o w [ i ] − 1 ) ∗ ( c o l [ j ] − 1 ) (row[i]-1)*(col[j]-1) (row[i] − 1) * (col[j] − 1), be careful to remove itself

#include<stdio.h>

int g[1005][1005];
int row[1005],col[1005];
int n,m;

int main()
{
	long long res = 0;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			scanf("%d",&g[i][j]);
			if(g[i][j]==1) 
			{
				row[i] ++;
				col[j] ++;
			 } 
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(g[i][j]==1)
				res += (row[i]-1)*(col[j]-1);
		}
	printf("%lld\n",res);
	return 0;
 } 

Tags: C Back-end

Posted on Fri, 22 Oct 2021 10:12:00 -0400 by aleczapka