2020.11.07 Beijing Normal University Freshman Competition (15) Question Solutions

1. P1980 counting problem

  • This is a sign-in question, just loop from 1 to n, and count how many times each number appears x.
n,x=map(int,input().split(' '))
count=0
for i in range(1,n+1):
    a,b,c=0,i,i
    while(c>0):
        if x == (c%10):
            count+=1
        c//=10
print(count)

2.CF50A Domino piling

  • For this question, if N ∗ MN * MNIf M is an even number, then the answer must beN ∗ M / 2 N * M / 2NM / 2 , if it is an odd number, it isN ∗ M / 2 − 1 N * M / 2-1NM/21 . Of course, considering that C++ division is an even division, so directly outputN ∗ M / 2 N * M / 2NM / 2 is fine, if it is python, outputN ∗ M / / 2 N * M // 2NM//2.
#include<iostream>
using namespace std;
int M, N;
int main() {
    
    
	cin >> M >> N;
	cout << M * N / 2 << endl;
	return 0;
}

3.CF749A Bachgold Problem

  • If N is an even number, it will be decomposed into 2; if N is an odd number, it will be decomposed into a lot of 2 and one 3.
#include<cstdio>
int main() {
    
    
	int n;
	scanf("%d", &n);
	if (n % 2) {
    
    
		n /= 2;
		printf("%d\n", n);
		for (int i = 0; i + 1 < n; i++) {
    
    
			printf("%d ", 2);
		}
		printf("%d\n", 3);
	}
	else {
    
    
		n /= 2;
		printf("%d\n", n);
		for (int i = 0; i < n; i++) {
    
    
			printf("%d%c", 2, i + 1 == n ? '\n' : ' ');
		}
	}
	return 0;
}

4.CF1189B Number Circle

  • This question is to sort the data first, then put the smallest number first from small to large, and then put a number on each side from small to large, for example: 1, 2, 3. So, first put 1; then put 1, 2; then put 3, 1, 2
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100005], ans[100005];
int main() {
    
    
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
    
    
		scanf("%d", &a[i]);
	}
	sort(a, a + N);
	int p1 = 0, p2 = N - 1;
	int i = 0;
	while (i < N) {
    
    
		ans[p1++] = a[i++];
		if (i < N) ans[p2--] = a[i++];
	}
	bool flag = true;
	for (int i = 0; i < N; i++) {
    
    
		if (ans[i] >= ans[(i + N - 1) % N] + ans[(i + 1) % N]) {
    
    
			flag = false;
			break;
		}
	}
	if (flag) {
    
    
		printf("YES\n");
		for (int i = 0; i < N; i++) {
    
    
			printf("%d%c", ans[i], i + 1 == N ? '\n' : ' ');
		}
	}
	else {
    
    
		printf("NO\n");
	}
	return 0;
}

5.CF377A Maze

  • This is a very interesting construction problem. Assuming that the number of open spaces is cnt and the number of walls that need to be placed is K, then start from an open space with cnt-K spaces, and then put the remaining K on the wall. In this way, it can be ensured that the remaining cnt-K spaces are connected.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int N, M, K;
const int maxn = 510;
char maze[maxn][maxn];
int dx[] = {
    
    1, -1, 0, 0}, dy[] = {
    
    0, 0, 1, -1};
int cnt, blank;
void dfs(int x, int y){
    
    
	maze[x][y] = '*';
	for(int i = 0; i < 4; i++){
    
    
		int nx = x + dx[i], ny = y + dy[i];
		if(nx < 0 || nx >= N || ny < 0 || ny >= M || maze[nx][ny] != '.') continue;
		if(++blank <= cnt - K) dfs(nx, ny);
	}
}
int main(){
    
    
	scanf("%d%d%d", &N, &M, &K);
	for(int i = 0; i < N; i++){
    
    
		scanf("%s", maze[i]);
	}

	for(int i = 0; i < N; i++){
    
    
		for(int j = 0; j < M; j++){
    
    
			if(maze[i][j] == '.') cnt++;
		}
	}

	bool flag = false;
	for(int i = 0; i < N; i++){
    
    
		for(int j = 0; j < M; j++){
    
    
			if(maze[i][j] == '.' && ++blank <= cnt - K){
    
    
				dfs(i, j);
				flag = true;
				break;
			}
		}
		if(flag) break;
	}


	for(int i = 0; i < N; i++){
    
    
		for(int j = 0; j < M; j++){
    
    
			if(maze[i][j] == '*') maze[i][j] = '.';
			else if(maze[i][j] == '.') maze[i][j] = 'X';
		}
	}
	for(int i = 0; i < N; i++) printf("%s\n", maze[i]);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45812711/article/details/109343742