当前位置:网站首页>CDZSC_ 2022 winter vacation personal training match level 21 (2)

CDZSC_ 2022 winter vacation personal training match level 21 (2)

2022-07-07 09:47:00 moyangxian

A

Answer key : Output n, 1, 2, 3, 4… that will do .

#include<bits/stdc++.h>

using namespace std;

void solve() {
    
	int n;
	scanf("%d", &n);
	printf("%d", n);
	for (int i = 2; i <= n; i++)
		printf(" %d", i - 1);
	printf("\n");
}

signed main() {
    
	int T;
	scanf("%d", &T);
	while (T--) solve();
	return 0;
}

B

Answer key : First of all, it can be thought that the number of qualified people is very small , All in all 81 individual ; So directly enumerate these numbers to determine whether they are less than or equal to n that will do .

#include<bits/stdc++.h>

using namespace std;

void solve() {
    
	int n;
	scanf("%d", &n);
	int ans = 0;
	for (int i = 1; i <= 9; i++) {
    
		int x = 0;
		for (int j = 1; j <= 9; j++) {
    
			x = x * 10 + i;
			if (n >= x) ans++;
		}
	}
	printf("%d\n", ans);
}

signed main() {
    
	int T;
	scanf("%d", &T);
	while (T--) solve();
	return 0;
}

C

Answer key : Imagine the process of moving the box to the right , such as :
4 2 Move right back –> 2 4
In fact, two numbers exchange positions , Then the answer is the array after sorting .

#include<bits/stdc++.h>

using namespace std;
const int N = 110;
int a[N];

signed main() {
    
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sort(a + 1, a + 1 + n);
	for (int i = 1; i <= n; i++) {
    
		if (i == 1) printf("%d", a[i]);
		else printf(" %d", a[i]);
	}
	printf("\n");
	return 0;
}

D

Answer key : Think in reverse , We can b Go to a To push , that b There are also two operations

  1. b Divide under the premise of even number 2
  2. b The last one is 1 Remove the last one under the premise of

These two operations are independent of each other , therefore b You can repeat these two operations until you can't operate or b<=a until , Finally, judge b Is it equal to a that will do .

#include<bits/stdc++.h>

using namespace std;

signed main() {
    
	int a, b;
	scanf("%d%d", &a, &b);
	vector<int> ans;
	while (b > a) {
    
		if (b & 1) {
    
			if (b % 10 == 1) {
    
				b /= 10;
				ans.push_back(10);
			}
			else break;
		}
		else {
    
			b >>= 1;
			ans.push_back(2);
		}
	}
	reverse(ans.begin(), ans.end());
	if (a == b) {
    
		printf("YES\n");
		printf("%d\n", ans.size() + 1);
		printf("%d", a);
		for (auto x : ans) {
    
			if (x == 10) a = a * 10 + 1;
			else a <<= 1;
			printf(" %d", a);
		}
	}
	else {
    
		printf("NO\n");
	}
	return 0;
}

E

Answer key : You can determine a minimum value first , Then delete the number smaller than it, and the difference between the sum and the minimum value is greater than d The number of gets the answer . Take every number in the array as the minimum , The answer is the minimum .

#include<bits/stdc++.h>

using namespace std;
const int N = 110;
int a[N];

signed main() {
    
	int n, d;
	scanf("%d%d", &n, &d);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sort(a + 1, a + 1 + n);
	int ans = n;
	for (int i = 1; i <= n; i++) {
    
		int cnt = n - 1;
		for (int j = i + 1; j <= n; j++)
			if (a[j] >= a[i] && a[j] - a[i] <= d) cnt--;
		ans = min(ans, cnt);
	}
	printf("%d\n", ans);
	return 0;
}

F

Knowledge point : Binary understanding

Answer key : First of all, will n Into binary form , Remove from high to low , See if it can be disassembled n The sum of each bit of is equal to k, For example, the first example of the title :
9 4

9 --> 1001
sum( Sum of all digits ) = 2

1001 --> 0201
sum = 3

0201 --> 0121
sum = 4

0121 = 0 * 23 + 1 * 22 + 2 * 21 + 1 * 20

#include<bits/stdc++.h>

using namespace std;
const int N = 100;
int a[N], tot;

signed main() {
    
	int n, k;
	scanf("%d%d", &n, &k);
	int sum = 0;
	while (n) {
    
		if (n & 1) sum++, a[++tot] = 1;
		else a[++tot] = 0;
		n >>= 1;
	}
	while (sum < k) {
    
		if (tot == 1) break;
		a[tot]--;
		a[tot - 1] += 2;
		sum++;
		if (a[tot] == 0) tot--;
	}
	if (sum != k) printf("NO\n");
	else {
    
		printf("YES\n");
		for (int i = 1; i <= tot; i++) {
    
			for (int j = 1; j <= a[i]; j++)
				printf("%d ", (1 << (i - 1)));
		}
		printf("\n");
	}
	return 0;
}

G

Answer key : First of all, we press odd and even numbers 1~n Split into two arrays , Output the odd array in reverse order , Even array even[0] and even[1] Change the order and then output .
for example :n = 11
odd : 1 3 5 7 9 11 —> 11 9 7 5 3 1
even : 2 4 6 8 —> 4 2 6 8
ans : 11 9 7 5 3 1 4 2 6 8
The answer is not unique , There are many construction methods on the Internet , Search for

#include<bits/stdc++.h>

using namespace std;

void solve() {
    
	int n;
	scanf("%d", &n);
	if (n < 4) {
    
		printf("-1\n");
		return;
	}
	else {
    
		vector<int> odd, even;
		for (int i = 1; i <= n; i += 2)
			odd.push_back(i);
		for (int i = 2; i <= n; i += 2)
			even.push_back(i);
		reverse(odd.begin(), odd.end());
		for (int i = 0; i < odd.size(); i++)
			printf("%d ", odd[i]);
		printf("%d %d", even[1], even[0]);
		for (int i = 2; i < even.size(); i++)
			printf(" %d", even[i]);
		printf("\n");
	}
}

signed main() {
    
	int T;
	scanf("%d", &T);
	while (T--) solve();
	return 0;
}
原网站

版权声明
本文为[moyangxian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070709200344.html