24-2-22学习总结

文章讨论了两个编程问题:如何通过交换瓶子实现序列排序,以及如何在给定条件下切出最大尺寸的巧克力。涉及到了数组操作、循环和逻辑判断等算法技巧。
摘要由CSDN通过智能技术生成

练习题

P8637 [蓝桥杯 2016 省 B] 交换瓶子

# [蓝桥杯 2016 省 B] 交换瓶子

## 题目描述

有 $N$ 个瓶子,编号 $1 \sim N$,放在架子上。

比如有 $5$ 个瓶子:

$$2,1,3,5,4$$

要求每次拿起 $2$ 个瓶子,交换它们的位置。

经过若干次后,使得瓶子的序号为:

$$1,2,3,4,5$$

对于这么简单的情况,显然,至少需要交换 $2$ 次就可以复位。

如果瓶子更多呢?你可以通过编程来解决。

## 输入格式

第一行:一个正整数 $N$($N<10000$),表示瓶子的数目。

第二行:$N$ 个正整数,用空格分开,表示瓶子目前的排列情况。

## 输出格式

输出数据为一行一个正整数,表示至少交换多少次,才能完成排序。

## 样例 #1

### 样例输入 #1

```
5
3 1 2 5 4
```

### 样例输出 #1

```
3
```

## 样例 #2

### 样例输入 #2

```
5
5 4 3 2 1
```

### 样例输出 #2

```
2
```

## 提示

时限 1 秒, 256M。蓝桥杯 2016 年第七届省赛

蓝桥杯 2016 年省赛 B 组 I 题。

#include<bits/stdc++.h>
using namespace std;
const int MAX_INF = 0x3f3f3f3f;
const int M = 10001;
int ans, a[M], n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        if (a[i] != i)
        {
            ans++;
            for (int j = i+1; j <= n; j++)
            {
                if (a[j] == i)
                    swap(a[i], a[j]);
            }
        }
    }
    cout << ans;
    return 0;
}

P9230 [蓝桥杯 2023 省 A] 填空问题(没过)

# [蓝桥杯 2023 省 A] 填空问题

## 题目描述

## A. 幸运数

小蓝认为如果一个数含有偶数个数位,并且前面一半的数位之和等于后面一半的数位之和,则这个数是他的幸运数字。例如 $2314$ 是一个幸运数字,因为它有 $4$ 个数位,并且 $2+3=1+4$。现在请你帮他计算从 $1$ 至 $100000000$ 之间共有多少个不同的幸运数字。

## B. 有奖问答

小蓝正在参与一个现场问答的节目。活动中一共有 $30$ 道题目,每题只有答对和答错两种情况,每答对一题得 $10$ 分,答错一题分数归零。

小蓝可以在任意时刻结束答题并获得目前分数对应的奖项,之后不能再答任何题目。最高奖项需要 $100$ 分,所以到达 $100$ 分时小蓝会直接停止答题。

已知小蓝最终实际获得了 $70$ 分对应的奖项,请问小蓝所有可能的答题情况有多少种?

## 输入格式

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

您可以通过提交答案文件的方式进行提交。你需要将你的答案放在 `1.out` 和 `2.out` 中,其中 `1.out` 对应 A 题的答案,`2.out` 对应 B 题的答案。

输入文件包含一个字符,代表题目编号(`A` 或 `B`),你需要输出对应的答案。

以下模板供参考:

```cpp
#include<bits/stdc++.h>
using namespace std;

char pid;
int main() {
    cin >> pid;
    if(pid == 'A')
        puts("Answer for Problem A");
    else
        puts("Answer for Problem B");
    return 0;
}
```

## 输出格式

## 提示

第十四届蓝桥杯大赛软件赛省赛 C/C++ 大学 A 组 A-B

#include<bits/stdc++.h>
using namespace std;


int ans, b[10];
void PA()
{
	for (int k = 1; k <= 100000000; k++)
	{
		int g = 0, n = 0, m = k;
		for (; m; m /= 10)
		{
			b[n++] = m % 10;
		}
		if (n & 1) continue;
		for (int i = 0; i < n >> 1; ++i) g += b[i];
		for (int i = n >> 1; i < n; ++i) g -= b[i];
		if (!g) ++ans;
	}
	printf("%d", ans);
}
int nn = 30, anss = 0, target = 70;
void dfs(int depth, int score) 
{
		if (depth > nn) return;
		if (score == 100) return;
		if (score == target) ++anss;
		dfs(depth + 1, score + 10);
		dfs(depth + 1, 0);
}
void PB()
{
	printf("%d", (dfs(0, 0), anss));

}
char pid;
int main() {
	cin >> pid;
	if (pid == 'A')
	{
		PA();
	}
	else
	{
		PB();
	}
	return 0;
}

P8647 [蓝桥杯 2017 省 AB] 分巧克力

# [蓝桥杯 2017 省 AB] 分巧克力

## 题目描述

儿童节那天有 $K$ 位小朋友到小明家做客。小明拿出了珍藏的巧克力招待小朋友们。

小明一共有 $N$ 块巧克力,其中第 $i$ 块是 $H_i \times W_i$ 的方格组成的长方形。

为了公平起见,小明需要从这 $N$ 块巧克力中切出 $K$ 块巧克力分给小朋友们。切出的巧克力需要满足:

1. 形状是正方形,边长是整数。

2. 大小相同。

例如一块 $6 \times 5$ 的巧克力可以切出 $6$ 块 $2 \times 2$ 的巧克力或者 $2$ 块 $3 \times 3$ 的巧克力。

当然小朋友们都希望得到的巧克力尽可能大,你能帮小 $H_i$ 计算出最大的边长是多少么?

## 输入格式

第一行包含两个整数 $N$ 和 $K$。$(1 \le N,K \le 10^5)$。

以下 $N$ 行每行包含两个整数 $H_i$ 和 $W_i$。$(1 \le H_i,W_i \le 10^5)$。

输入保证每位小朋友至少能获得一块 $1 \times 1$ 的巧克力。

## 输出格式

输出切出的正方形巧克力最大可能的边长。

## 样例 #1

### 样例输入 #1

```
2 10  
6 5  
5 6
```

### 样例输出 #1

```
2
```

## 提示

蓝桥杯 2022 省赛 A 组 I 题。

#include<bits/stdc++.h>
using namespace std;
const int M = 100001;
int k, n, a[M], b[M];
int func(int x)
{
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		ans += (a[i] / x) * (b[i] / x);
	}
	return ans >= k;
}
int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i] >> b[i];
	}
	int left = 1, right = 100000;
	while (left < right)
	{
		int mid = (left + right + 1) / 2;
		if (func(mid))
			left = mid;
		else
			right = mid - 1;
	}
	cout << left;

}

L-shapes

# L-shapes

## 题面翻译

L形在网格纸上形如下面的前四张图片。L形正好包含三个阴影单元(用*表示),可以以任何方式旋转。

现给你一个矩形网格。确定它是否仅包含L形,其中L形不能接触边或角,也就是说网格中的每个阴影单元正好是一个L形的一部分,并且没有两个L形通过边或角相邻。

例如,上图中的最后两个网格不满足条件,因为两个L形分别通过角和边缘接触。


如果网格满足条件,则输出“YES”,否则输出“NO”。

## 题目描述

An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by \*), which can be rotated in any way.

 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1722F/c4bb8e8c5c94521e87ed2d5ba5ef373384bb7be7.png)You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally:

- Each shaded cell in the grid is part of exactly one L-shape, and
- no two L-shapes are adjacent by edge or corner.

For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.

## 输入格式

The input consists of multiple test cases. The first line contains an integer $ t $ ( $ 1 \leq t \leq 100 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $ n $ and $ m $ ( $ 1 \leq n, m \leq 50 $ ) — the number of rows and columns in the grid, respectively.

Then $ n $ lines follow, each containing $ m $ characters. Each of these characters is either '.' or '\*' — an empty cell or a shaded cell, respectively.

## 输出格式

For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

## 样例 #1

### 样例输入 #1

```
10
6 10
........**
.**......*
..*..*....
.....**...
...*.....*
..**....**
6 10
....*...**
.**......*
..*..*....
.....**...
...*.....*
..**....**
3 3
...
***
...
4 4
.*..
**..
..**
..*.
5 4
.*..
**..
....
..**
..*.
3 2
.*
**
*.
2 3
*..
.**
3 2
..
**
*.
3 3
.**
*.*
**.
3 3
..*
.**
..*
```

### 样例输出 #1

```
YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
```

#include<bits/stdc++.h>
using namespace std;
int T, n, m, a[55][55];
char c;
void search(int i, int j)
{
	if (a[i + 1][j] && a[i + 1][j + 1] && !a[i - 1][j - 1] && !a[i - 1][j] && !a[i - 1][j + 1] &&
		!a[i][j - 1] && !a[i][j + 1] && !a[i][j + 2] && !a[i + 1][j - 1] && !a[i + 1][j + 2] &&
		!a[i + 2][j - 1] && !a[i + 2][j] && !a[i + 2][j + 1] && !a[i + 2][j + 2]) {
		a[i][j] = a[i + 1][j] = a[i + 1][j + 1] = 0;
		return;
	}
	if (a[i + 1][j - 1] && a[i + 1][j] && !a[i - 1][j - 1] && !a[i - 1][j] && !a[i - 1][j + 1] &&
		!a[i][j - 2] && !a[i][j - 1] && !a[i][j + 1] && !a[i + 1][j - 2] && !a[i + 1][j + 1] &&
		!a[i + 2][j - 2] && !a[i + 2][j - 1] && !a[i + 2][j] && !a[i + 2][j + 1]) {
		a[i][j] = a[i + 1][j - 1] = a[i + 1][j] = 0;
		return;
	}
	if (a[i][j + 1] && a[i + 1][j + 1] && !a[i - 1][j - 1] && !a[i - 1][j] && !a[i - 1][j + 1] &&
		!a[i - 1][j + 2] && !a[i][j - 1] && !a[i][j + 2] && !a[i + 1][j - 1] && !a[i + 1][j] &&
		!a[i + 1][j + 2] && !a[i + 2][j] && !a[i + 2][j + 1] && !a[i + 2][j + 2]) {
		a[i][j] = a[i][j + 1] = a[i + 1][j + 1] = 0;
		return;
	}
	if (a[i][j + 1] && a[i + 1][j] && !a[i - 1][j - 1] && !a[i - 1][j] && !a[i - 1][j + 1] &&
		!a[i - 1][j + 2] && !a[i][j - 1] && !a[i][j + 2] && !a[i + 1][j - 1] && !a[i + 1][j + 1] &&
		!a[i + 1][j + 2] && !a[i + 2][j - 1] && !a[i + 2][j] && !a[i + 2][j + 1]) {
		a[i][j] = a[i][j + 1] = a[i + 1][j] = 0;
		return;
	}
}
void solve()
{
	cin >> n >> m;
	memset(a, 0, sizeof(a));
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> c;
			if (c == '*')
				a[i][j] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (a[i][j] == 1)
				search(i, j);
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (a[i][j] == 1)
			{
				printf("NO\n");
				return;
			}
			
		}
	}
	printf("YES\n");
}
int main()
{
	cin >> T;
	while (T--)
		solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值